volatile同步机制

 

1 java的同步机制synchronized,和Volatile;Volatile的开销较小,适用于读写比例很大的场景;非原子性,修改不能依赖于当前值;可以保证在多线程环境下,一个线程对变量的修改之后,其他线程看到这个变量的最新值(强迫线程读取共享内存中的值,而不是线程的本地拷贝)。

http://www.ibm.com/developerworks/cn/java/j-jtp06197.html

http://qingfeng825.iteye.com/blog/152269

Locking can guarantee both visibility and atomicity; volatile variables can only guarantee visibility.

 

 

You can use volatile variables only when all the following criteria are met: 

•  Writes to the variable do not depend on its current value, or you can ensure that only a single thread ever 

updates the value; 

•  The variable does not participate in invariants with other state variables; and 

•  Locking is not required for any other reason while the variable is being accessed. 

你可能感兴趣的:(并发)