java内存模型 多线程
synchronized
volatile variable read/write
explicit lock
原子变量
monitor each object
use of classes in the java.util.concurrent package
http://www.cs.umd.edu/~pugh/java/memoryModel/
https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html
every object
, in addition to having associated monitor
, has an associated wait set
. A wait set is a set of threads.
Object.wait
Object.notify
Object.notifyAll
rules for which values may be seen by a read of shared memory that is updated by multiple threads.
Java programming language memory model
Synchronization
communicating between threads
synchronization
which is implemented using monitors, which a thread can lock or unlock.
一个线程持有多把锁,可能会死锁。
synchronization方法:
- reads and writes of volatile variables
- the use of classes in the java.util.concurrent package
内存模型Memory Model
- 一个线程内,只有不影响最终结果,可以乱序执行
- 对于共享内存,每个线程有一份缓存,如果不是volatile变量,可能不更新缓存值
- a data race
一个线程读
一个线程写
and the write and read are not ordered by synchronization