Lock Release-Get established happens before relationship
Locks are the most important synchronization mechanism in Java concurrent programming. The lock allows the thread that releases the lock to send a message to the thread that acquires the same lock, except for the exclusion of the critical zone from execution.
Here is the sample code for lock Release-get:
Class Monitorexample { int a = 0; Public synchronized void writer () { //1 a++; 2 } //3 public synchronized void Reader () { //4 int i = A; 5 ... } 6}
Assume that thread a executes the writer () method, and then thread B executes the reader () method. According to the happens before rule, this process contains a happens before relationship that can be divided into two categories:
- According to the Rules of Procedure order, 1 happens before 2, 2 happens before 3; 4 happens before 5, 5 happens before 6.
- According to the monitor lock rule, 3 happens before 4.
- According to the transitivity of happens before, 2 happens before 5.
The graphical representation of the above happens before relationship is as follows:
In, each arrow links the two nodes that represent a happens before relationship. The black arrows represent the program order rules, the orange arrows represent the monitor lock rules, and the blue arrows indicate the happens before guarantees provided after the rules are combined.
Transfer from:http://www.infoq.com/cn/articles/java-memory-model-5&
"Go" in-depth understanding of Java memory Model (v)--lock