Wiki Markup |
---|
The Java Language Specification defines the Java Memory Model (JMM) which describes possible behaviors of a multi-threaded Java program. Memory that can be shared between threads is called _shared memory_ or _heap memory_. The term _variable_ is used in the context of this guideline, to refer to both fields and array elements \[[JLS 05|AA. Java References#JLS 05]\]. |
All instance fields, static
fields, and array elements are stored in heap memory. Local variables, formal method parameters, or exception handler parameters are never shared between threads and are not affected by the memory model.
The Java Language Specification defines the Java Memory Model (JMM) which describes possible behaviors of a multi-threaded Java program. Concurrent executions are typically interleaved but the situation is complicated by statements that may be reordered by the compiler or runtime system. This results in execution orders that are not immediately obvious from an examination of the source-code.
There are two requirements for implementing synchronization correctly:
1. The happensHappens-before consistency: If two accesses follow the happens-before relationship, data races cannot occur. However, this is necessary but not sufficient for acceptable program behavior. In addition, often the particular execution order of a program must be sequential consistent.
...
In this ordering, r1
and r2
read the original values of the variables a
and b
even though they might be are expected to see the updated values.
...
Wiki Markup |
---|
2. [Sequential consistency|BB. Definitions#sequential consistency]: "The fact that we allow a read to see a write that comes later in the execution order can sometimes thus result in unacceptable behaviors." \[[JLS 05|AA. Java References#JLS 05]\]. In such cases, sequential consistency is required. This condition ensures that the compiler does not optimize away or reorder any statements. It also ensures that each operation is atomic and immediately visible to other threads. This makes it easy for a programmer to follow the logic, however, introduces a performance penalty. Synchronization guarantees sequential consistency and so does use of the {{volatile}} keyword. |
Declaring a variable volatile
guarantees the happens-before relationship so that writes are always visible to subsequent reads from any thread. It also ensures sequential consistency, in that, volatile read and write operations cannot be reordered with respect to each other and in addition, as required by the modern JMM, volatile read and write operations are also not reordered with respect to operations on non-volatile variables.
...
This noncompliant code example uses a shutdown()
method to set a non-volatile done
flag that is checked in the run()
method. If some thread invokes the shutdown()
method to set the flag, it is possible that another thread might not observe this change. Consequently, it may be forced to sleep even though the condition variable (or flag) disallows this.
Code Block | ||
---|---|---|
| ||
final class ControlledStop implements Runnable { private boolean done = false; public void run() { while(!done) { try { // ... Thread.currentThread().sleep(1000); // Do something } catch(InterruptedException ie) { // handle } } } protected void shutdown(){ done = true; } } |
...