Wiki Markup |
---|
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]\]. Variables that are shared between threads are referred to as shared variables. All instance fields, {{static}} fields, and array elements are shared variables and 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|BB. Definitions#memory model]. |
The Java Language Specification defines the Java Memory Model (JMM) which describes possible behaviors of a multi-threaded Java program. If the correctness of the constituting code is based on its execution characteristics in a single threaded environment, the code may be unsafe for use in a multi-threaded environment. Two kinds of hazards, visibiity and reordering, occur when special concurrency primitives are not used in such code.
Visibility refers to the requirement that every thread sees the value of the most recent update to a shared variable. This is essential in multi-threaded programs because the value of a shared variable may be cached and not written to main memory immediately. Consequently, another thread may retrieve a stale value of the variable when attempting to read it from the main memory. Using the volatile
keyword mitigates this risk and so does correct synchronization.
The restricted or no reordering requirement refers to ensuring that the execution sequence of a set of statements does not vary when observed by multiple threads. Concurrent executions of code are typically interleaved and statements may be reordered by the compiler or runtime system to facilitate various optimizations. This results in execution orders that are not immediately obvious from an examination of the source-code. Failure to account for this hazard is a common source of data races. Restricting the set of possible reorderings makes it easier to reason about the safety of the code. This risk is mitigated by correctly synchronizing the code and in some cases, by using the volatile
keyword.
Even if statements execute in the expected order (program order), caching can prevent the latest values from being reflected in the main memory (visibility hazard). Program order is the execution order that is expected when a single thread is running the statements sequentially, as written in a method.
There are two requirements for writing provably correct multi-threaded code:
1. Happens-before consistency: If two accesses of a shared variable follow the happens-before relationship, data races arising from statement reorderings cannot occur. However, this is necessary but not sufficient for acceptable program behavior.
Consider the following example in which a
and b
are (shared) global variables or instance fields but r1
and r2
are local variables not accessible by other threads.
Initially, let a = 0
and b = 0
.
|
|
---|---|
|
|
|
|
Because, in Thread 1
, the two assignments a = 10;
and r1 = b;
are not related, the compiler or runtime system is free to reorder them. Similarly in Thread 2
, the statements may be freely reordered. Although it may seem counter-intuitive, the Java memory model allows a read to see a write that occurs later in the execution order.
A possible execution order showing actual assignments is:
Execution Order | Assignment | Assigned Value | Notes |
---|---|---|---|
1. |
| 10 |
|
2. |
| 20 |
|
3. |
| 0 | Reads initial value of |
4. |
| 0 | Reads initial value of |
In this ordering, r1
and r2
read the original values of the variables a
and b
even though they are expected to see the updated values, 10 and 20. Another possible execution order showing actual assignments is:
Execution Order | Statement | Assigned Value | Notes |
---|---|---|---|
1. |
| 20 | Reads later value (in step 4.) of write, that is 20 |
2. |
| 10 | Reads later value (in step 3.) of write, that is 10 |
3. |
| 10 |
|
4. |
| 20 |
|
In this ordering, r1
and r2
read the values of a
and b
written from step 3 and 4, even before the statements corresponding to these steps have executed.
Wiki Markup |
---|
"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]\]. Both, the use of synchronization and the {{volatile}} keyword prevent a thread from observing inconsistent values of shared variables. |
2. Sequential consistency: This property provides a very strong guarantee that the compiler will not optimize away or reorder any statements. It guarantees that the program is free from data races. It also ensures that each access of a variable is atomic and immediately visible to other threads.
For example, consider a set of statements that are being executed by multiple threads:
Thread 1,2,3... |
---|
Statement 1 |
Statement 2 |
Statement 3 |
If statements 1, 2 and 3 are always executed sequentially by all threads as given in this program order, they are sequentially consistent with respect to each other. The sequential consistency property also requires that a read operation in some thread does not see the value of a future write operation taking place in the same or another thread. Similarly, a read operation is guaranteed to see the value of the last write to the variable from any thread.
The use of sequential consistency as the sole memory model mechanism makes it easy for a programmer to reason with the program's logic in a multithreading scenario, however, introduces a performance penalty because the compiler is prohibited from reordering code for performing complex optimizations. Using volatile
variables reduces this performance penalty at the cost of strong sequential consistency guarantees.
Consider two threads that are executing some statements:
...