关于`synchronized`同意。
关于可能并发错误的观察很好。以下是相关代码。特别地,`deref`使用了`volatile`/非同步策略。
```
public class Delay implements IDeref, IPending {
volatile Object value;
Object unsynchronizedValue;
ReentrantLock lock;
private static class ExceptionalValue {
private final Object v;
public ExceptionalValue (Object v) {
this.v = v;
}
}
private static class PendingValue {
private final Object v;
public PendingValue (Object v) {
this.v = v;
}
}
public Delay(IFn f){
Object v = new PendingValue(f);
unsynchronizedValue = v;
value = v;
lock = new ReentrantLock();
}
private static Object getOrThrow(Object v) {
if (v instanceof ExceptionalValue) {
return Util.sneakyThrow((Throwable)(((ExceptionalValue)v).v));
} else {
return v;
}
}
public Object deref() {
if (unsynchronizedValue instanceof PendingValue) {
volatile读取
Object v = value;
if (v instanceof PendingValue) {
try {
lock.lock();
volatile读取在锁定后,以供变更使用
Object vAfter = value;
如果(vAfter instanceof PendingValue) {
IFn f = (IFn)((PendingValue)vAfter).v;
Object vComputed = null;
try {
vComputed = f.invoke();
} catch (Throwable t) {
vComputed = new ExceptionalValue(t);
unsynchronizedValue = vComputed;
value = vComputed;
return getOrThrow(vComputed);
} else {
return getOrThrow(vAfter);
}
>finally {
lock.unlock();
}
else {
return v;
} else {
}
}
}
... // 在这里实现`force`,`isRealized`
}
```