https://shipilev.net/blog/2014/all-fields-are-final/

https://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html

Doug Lea : JMM Cookbook http://gee.cs.oswego.edu/dl/jmm/cookbook.html

对 reference 类型来说,JMM 更多意味着安全发布,即将对象赋值给使用方时,final, volatile, synchronized 语义附带的 (StoreLoad 和 StoreStore) 可以初始化字段被正确读取。

volatile 适用于状态更换,是 CAS 操作的基础,衍生出了 atomic 和 AQS。

线程同步也很有意思。

规范语义

volatile

禁指令重排

某一 CPU 操作 volatile 修饰变量后,其变化变化会立即对其他 CPU 可见

大部分语义依赖于底层硬件,不是 100% 保证正确。

volatile

This only applies for two threads with a write-read relationship on the same field!

  1. When a thread writes to a volatile variable, all of its previous writes are guaranteed to be visible to another thread when that thread is reading the same value.
  2. Both threads must align "their" volatile value with that in main memory.