First, the Primer
If all the ordering in the Java memory model is done only with volatile and synchronized, then there are some operations that can become cumbersome, but we do not feel this when writing Java concurrency code, This is because there is an antecedent principle (Happens-before) In the Java language, and through this principle, we can solve all the problems that can conflict between the two operations in the concurrency environment through a few rules package.
Second, the definition
The first occurrence is a partial-order relationship between two errors defined in the Java memory model, and if operation a precedes and operates B, it is said that the effect of operation A can be observed by Operation B before Operation B occurs, "Impact" includes modifying the values of shared variables in memory, sending messages, calling methods, and so on.
III. rules
The "natural" first-sex rules in the Java memory model are as follows:
1. Rules of Procedure Order:
In a thread, according to the sequence of program code, written in front of the operation first occurs in the operation of the following, accurately speaking, should be control flow program rather than program code order, because to consider the structure of branch, loop, etc.
2. Cheng Rules:
A unlock operation first occurs after the lock operation facing the same lock, it must be emphasized that the same lock, "back" refers to the chronological order.
3. Volatile variable rules:
The write operation of a volatile variable precedes the read operation of the variable, and the "Back" is also the chronological order of the time.
4. Thread Start rule:
The start () method of the Thread object occurs first in every action of the secondary thread;
5. Thread Termination rule:
All operations in the thread occur first in the termination detection of this thread and can be detected by means of the Thread.Join () method end, the return value of thread.isalive (), and so on, to detect that the thread has terminated execution
6. Thread Break rule:
The invocation of the thread interrupt () method first occurs when the code of the interrupted thread detects that the interrupt event occurred, and can be detected by the THREAD.INTERRUPTD () method.
7. Object End rule:
The initialization of an object (the completion of a constructor execution) precedes the start of its finalize () method
8, transmission of
If operation a precedes operation B, and action B first occurs in Operation C, then it can be assumed that operation a precedes operation C.
Iv. examples
The following cases:
public class TestValue { private int value = 0; public int GetValue () { return value; } public void SetValue (int value) { this.value = value; }}
If thread A calls "SetValue (1)" and thread B calls the "GetValue ()" Method of the same object, what is the return value?
The answer is not sure, because the operation here is not thread-safe, and it does not meet all of the preceding rules.
There are many ways to fix this, and here are two simple examples:
1) Define the Getter/setter method as the Synchronized method
2) Define value as a volatile variable.
Java multi-thread first occurrence principle