【发布时间】:2014-03-28 07:31:02
【问题描述】:
这是一个简单的例子:
private long counter = 0;
// note this method is NOT synchronized
// this will be called by thread A
public void increment() { counter++; }
// note this method IS synchronized
// this will be called by thread B
public synchronized long value() { return counter; }
所以我只想为counter 获得一个好的值,而不是cpu 缓存中的卡住值,因为该变量是非易失性的。目标是不使计数器易失,因此它不会影响执行增量的线程 A,而只会影响线程 B,我不在乎,当它读取变量时。
为了记录,我打算在线程 A 已经完成时从线程 B 读取 counter 的值...
【问题讨论】:
标签: java multithreading concurrency real-time java-memory-model