第一个问题:
为什么outcome变量不加上volatile,以及在set和setException方法中,要专门的UNSAFE.putOrderedInt 已经加上volatile的state方法?
因为我个人认为,outcomme会被多个线程访问(一个可以读写,其他的只能读),这种情况下,为啥不加上volatile,加上volatile的好处可以让outcome和state变量被修改后,其他线程立刻知道?而为什么作者故意不使用volatile的特性呢?
/** The result to return or exception to throw from get() */
private Object outcome; // non-volatile, protected by state reads/writes
protected void set(V v) {
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
outcome = v;
UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
finishCompletion();
}
}
猜测:
state已经是volatile,写outcome happen-before 写state,根据内存模型别的线程看到state=NORMAL时应该也看到最新的outcome