标签:cts memory ram lis java学习 new ever cep ogr
"The Java Language Specification Java SE 12 Edition"
17.4.4 Synchronization Order
Every execution has a synchronization order. A synchronization order is a total
order over all of the synchronization actions of an execution. For each thread t,
the synchronization order of the synchronization actions (§17.4.2) in t is consistent
with the program order (§17.4.3) of t.
Synchronization actions induce the synchronized-with relation on actions, defined
as follows:
? An unlock action on monitor m synchronizes-with all subsequent lock actions on
m (where "subsequent" is defined according to the synchronization order).
? A write to a volatile variable v (§8.3.1.4) synchronizes-with all subsequent
reads of v by any thread (where "subsequent" is defined according to the
synchronization order).
? An action that starts a thread synchronizes-with the first action in the thread it
starts.THREADS AND LOCKS Memory Model 17.4
675
? The write of the default value (zero, false, or null) to each variable
synchronizes-with the first action in every thread.
Although it may seem a little strange to write a default value to a variable before the
object containing the variable is allocated, conceptually every object is created at the
start of the program with its default initialized values.
? The final action in a thread T1 synchronizes-with any action in another thread T2
that detects that T1 has terminated.
T2 may accomplish this by calling T1.isAlive() or T1.join().
? If thread T1 interrupts thread T2, the interrupt by T1 synchronizes-with any point
where any other thread (including T2) determines that T2 has been interrupted (by
having an InterruptedException thrown or by invoking Thread.interrupted
or Thread.isInterrupted).
The source of a synchronizes-with edge is called a release, and the destination is
called an acquire
对于加锁解锁同步关系的理解
An unlock action on monitor m synchronizes-with all subsequent lock actions on
m (where "subsequent" is defined according to the synchronization order).
private static volatile int sharedData; private static final Object LOCK = new Object(); public static void changeData(int data) { /** * * An unlock action on monitor m synchronizes-with all subsequent lock actions on * m (where "subsequent" is defined according to the synchronization order). * 对于同步关系中这句话的理解为: * 存在多个不同的竞争线程来准备持有LOCK; 此时T1获取到了锁, 而其他的线程处于等待LOCK monitor release中; * 此时 T1释放 LOCK, 此时其他的线程中只会有一个线程T2获取到锁; * 对于T1 unlock(LOCK) -> T2 lock(LOCK) 对于这两个关系实际是同步的; 这里实际并不会去考虑其他线程竞争当前资源的过程,对于上一个线程解锁和下一个线程加锁实际就是同步的 * */ synchronized (LOCK) { sharedData = data; } }
java学习-JMM-synchronization-order
标签:cts memory ram lis java学习 new ever cep ogr
原文地址:https://www.cnblogs.com/xingguoblog/p/13801213.html