FutureTask中outcome为何不使用volatile修饰
来源:12-7 用法二:用FutureTask获取结果

程序员不会说方言
2020-05-20
老师你好,FutureTask源码中有一个用于返回的私有域outcome:
/** The result to return or exception to throw from get() */
private Object outcome; // non-volatile, protected by state reads/writes
注释上说由于state被volatile修饰保证了outcome无需被volatile修饰,我不是很明白,难道是happens-before原则吗,但我看源码中outcome的获取都是在state赋值后发生的,不是之前呀, 如下:
public V get() throws InterruptedException, ExecutionException {
int s = state;
if (s <= COMPLETING)
s = awaitDone(false, 0L);
return report(s);
}
private V report(int s) throws ExecutionException {
Object x = outcome;
if (s == NORMAL)
return (V)x;
if (s >= CANCELLED)
throw new CancellationException();
throw new ExecutionException((Throwable)x);
}
写回答
1回答
-
10
相似问题