Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

 

Reading a shared primitive variable in one thread may not yield the value of the most recent write to the variable from another thread. Consequently, the thread may observe a stale value of the shared variable. To ensure the visibility of the most recent update, either the variable must be declared volatile or the reads and writes must be synchronized.

...

This noncompliant code example uses a shutdown() method to set the non-volatile done flag that is checked in the run() method.

Code Block
bgColor#FFcccc

final class ControlledStop implements Runnable {
  private boolean done = false;
 
  @Override public void run() {
    while (!done) {
      try {
        // ...
        Thread.currentThread().sleep(1000); // Do something
      } catch(InterruptedException ie) { 
        Thread.currentThread().interrupt(); // Reset interrupted status
      } 
    } 	 
  }

  public void shutdown() {
    done = true;
  }
}

...

In this compliant solution, the done flag is declared volatile to ensure that writes are visible to other threads.

Code Block
bgColor#ccccff

final class ControlledStop implements Runnable {
  private volatile boolean done = false;
 
  @Override public void run() {
    while (!done) {
      try {
        // ...
        Thread.currentThread().sleep(1000); // Do something
      } catch(InterruptedException ie) { 
        Thread.currentThread().interrupt(); // Reset interrupted status
      } 
    } 	 
  }

  public void shutdown() {
    done = true;
  }
}

...

In this compliant solution, the done flag is declared to be of type java.util.concurrent.atomic.AtomicBoolean. Atomic types also guarantee that writes are visible to other threads.

Code Block
bgColor#ccccff

final class ControlledStop implements Runnable {
  private final AtomicBoolean done = new AtomicBoolean(false);
 
  @Override public void run() {
    while (!done.get()) {
      try {
        // ...
        Thread.currentThread().sleep(1000); // Do something
      } catch(InterruptedException ie) { 
        Thread.currentThread().interrupt(); // Reset interrupted status
      } 
    } 	 
  }

  public void shutdown() {
    done.set(true);
  }
}

...

This compliant solution uses the intrinsic lock of the Class object to ensure that updates are visible to other threads.

Code Block
bgColor#ccccff

final class ControlledStop implements Runnable {
  private boolean done = false;
 
  @Override public void run() {
    while (!isDone()) {
      try {
        // ...
        Thread.currentThread().sleep(1000); // Do something
      } catch(InterruptedException ie) { 
        Thread.currentThread().interrupt(); // Reset interrupted status
      } 
    } 	 
  }

  public synchronized boolean isDone() {
    return done;
  }

  public synchronized void shutdown() {
    done = true;
  }
}

...

Some static analysis tools are capable of detecting violations of this rule.

 ToolVersion Description 
 Eclipse 4.2.0 Not Implemented
 FindBugs 2.0.1  Not Implemented
 PMD 5.0.0  Not Implemented
 Fortify   Not Implemented
 Coverity  Fully Implemented

Related Guidelines

MITRE CWE

CWE-667. Improper locking

 

CWE-413. Improper resource locking

 

CWE-567. Unsynchronized access to shared data in a multithreaded context

...

[Bloch 2008]

Item 66. Synchronize access to shared mutable data

[Goetz 2006]

3.4.2, Example: Using Volatile to Publish Immutable Objects

[JLS 2005]

Chapter 17, Threads and Locks

 

§17.4.5, Happens-Before Order

 

§17.4.3, Programs and Program Order

 

§17.4.8, Executions and Causality Requirements

[JPL 2006]

14.10.3, The Happens-Before Relationship

 

07. Visibility and Atomicity (VNA)      07. Visibility and Atomicity (VNA)