volatile
在我有两个线程读取/写入它并且不希望取出锁的开销(或潜在的死锁风险)的情况下,我偶尔会使用实例变量;例如,一个计时器线程定期更新一个 int ID,该 ID 作为某个类的 getter 公开:
public class MyClass {
private volatile int id;
public MyClass() {
ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
execService.scheduleAtFixedRate(new Runnable() {
public void run() {
++id;
}
}, 0L, 30L, TimeUnit.SECONDS);
}
public int getId() {
return id;
}
}
我的问题:鉴于 JLS 只保证 32 位读取是原子的,那么使用 volatile long 有什么意义吗?(即 64 位)。
警告:请不要回复说使用volatile
oversynchronized
是预优化的情况;我很清楚如何/何时使用synchronized
,但在某些情况下volatile
更可取。例如,在定义用于单线程应用程序的 Spring bean 时,我倾向于使用volatile
实例变量,因为不能保证 Spring 上下文会在主线程中初始化每个 bean 的属性。