Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Checked for normative text; minor grammar fixes to improve precision

...

This code remains unsuitable for multithreaded use because declaring a variable as volatile does not fails to guarantee the atomicity of compound operations on itthe variable.

Compliant Solution (Synchronization)

...

Code Block
bgColor#ccccff
final class Flag {
  private volatile boolean flag = true;

  public synchronized void toggle() {
    flag ^= true; // Same as flag = !flag;
  }

  public boolean getFlag() {
    return flag;
  }
}

Wiki Markup
This approach mayis notforbidden befor usedgetter whenmethods athat getterperform methodany performsadditional operations other than just returning the value of a volatile field without having to use anyof synchronization. Unless read performance is critical, this technique may not offerlack significant advantages over synchronization \[[Goetz 2006|AA. Bibliography#Goetz 06]\].

...

In this noncompliant code example, multiple threads can invoke the setValues() method to set the a and b fields. Because this class does not fails to test for integer overflow, a user users of the Adder class must ensure that the arguments to the setValues() method can be added without overflow. (For more information, see guideline NUM00-J. Detect or prevent integer overflow.)

...

The getSum() method contains a race condition. For example, if when a and b currently have the values 0 and Integer.MAX_VALUE, respectively, and one thread calls getSum() while another calls setValues(Integer.MAX_VALUE, 0), the getSum() method might return either 0 or Integer.MAX_VALUE, or it might overflow and wrap. Overflow will occur when the first thread reads a and b after the second thread has set the value of a to Integer.MAX_VALUE, but before it has set the value of b to 0.

Note that declaring the variables as volatile does not fails to resolve the issue because these compound operations involve reads and writes of multiple variables.

...

The simple replacement of the two int fields with atomic integers, in this example, does not fails to eliminate the race condition because the compound operation a.get() + b.get() is still non-atomic.

...

Code Block
bgColor#ccccff
final class Adder {
  private int a;
  private int b;

  public synchronized int getSum() {
    return a + b;
  }

  public synchronized void setValues(int a, int b) {
    this.a = a;
    this.b = b;
  }
}

Any The operations within the synchronized methods are now atomic , with respect to other synchronized methods that lock on that object's monitor (intrinsic lock). It is now possible, for example, to add overflow checking to the synchronized getSum() method without introducing the possibility of a race condition.

Risk Assessment

If When operations on shared variables are not atomic, unexpected results can be produced. For example, information can be disclosed inadvertently because one user can receive information about other users.

...

The SureLogic Flashlight tool can diagnose violations of this guideline as instance fields with empty locksets.

Automated Detection

The Coverity Prevent Version 5.0 ATOMICITY checker can detect the instances of non-atomic update of a concurrently shared value. The result of the update will be determined by the interleaving of thread execution. The GUARDED_BY_VIOLATION checker can detect the instances where thread shared data is accessed without holding an appropriate lock, possibly causing a race condition.

...

Wiki Markup
\[[API 2006|AA. Bibliography#API 06]\] Class AtomicInteger
\[[JLSBloch 20052008|AA. Bibliography#JLSBibliography#Bloch 0508]\] Item 66: Synchronize access to [Chapter 17, Threads and Locks|http://java.sun.com/docs/shared mutable data
\[[Goetz 2006|AA. Bibliography#Goetz 06]\] 2.3. "Locking"\[[JLS 2005|AA. Bibliography#JLS 05]\] [Chapter 17, Threads and Locks|http://java.sun.com/docs/books/jls/third_edition/html/memory.html], Section 17.4.5 Happens-Before Order, Section 17.4.3 Programs and Program Order, Section 17.4.8 Executions and Causality Requirements
\[[TutorialsLea 20082000|AA. Bibliography#TutorialsBibliography#Lea 0800]\] [Java Concurrency Tutorial|http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html]
\[[Lea 2000|AA. Bibliography#Lea 00]\] Section 2.2.Section 2.2.7 The Java Memory Model, Section 2.1.1.1 Objects and Locks
\[[BlochTutorials 2008|AA. Bibliography#BlochBibliography#Tutorials 08]\] Item[Java 66: Synchronize access to shared mutable data
\[[Goetz 2006|AA. Bibliography#Goetz 06]\] 2.3. "Locking"Concurrency Tutorial|http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html]

Issue Tracking

Tasklist
Review List
Review List
||Completed||Priority||Locked||CreatedDate||CompletedDate||Assignee||Name||

...