``````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();
}
}

``````

## 第一重解析

hb(outcome=v , get outcome) 那么我们就可以得出 outcome一定可以被其他线程可见。

#### 以下下翻译一个经典的例子(来源于引用1)：

``````volatile int state;

Integer result;

void succeed(Integer result)
if(state==PENDING)              vr0
this.result = result;        w1
state = DONE;               vw1

Integer peekResult()
if(state==DONE)                 vr2
return result;               r2
return null;
``````

w1 -> vw1 -> vr2 -> r2

``````void succeed(Integer result)
if( compareAndSet(state, PENDING, DONE) )      vr0+vw0
this.result = result;                       w1
``````

``````void succeed(Integer result)
if(state==PENDING)         vr0
state=DONE;            vw0
this.result = result;   w1
``````

``````void succeed(Integer result)
if(state==PENDING)            vr0
state=TMP;                vw0
this.result = result;      w1
state=DONE;               vw1
``````

``````void succeed(Integer result)
if( compareAndSet(state, PENDING, TMP) )       vr0+vw0
this.result = result;                       w1
state=DONE;                                vw1
``````

``````private volatile int state;
private Object outcome;

protected void set(V v) {
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
outcome = v;
UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
finishCompletion();
}
}
private int awaitDone(boolean timed, long nanos) {
xxxxxxx

int s = state;
if (s > COMPLETING) {
if (q != null)
return s;
}
xxxxxxx
}
}

``````

``````UNSAFE.putOrderedInt(this, stateOffset, NORMAL);
``````

``````volatile int state;
int tmp;
Object outcome;

void set(){
if( compareAndSet(state, COMPLETING, TMP) )
outcome = v;
tmp = NORMAL;
}
void awaitDone(){
if(state==COMPLETING && tmp==NORMAL){
//xxxx
}
}
``````

### 第二重解析

1. 调用get方法线程一定知道state已经从`NEW`变成`COMPLETING`
`UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)`
2. 根据`程序次序规则`，当get方法线程知道`state==NORMAL`时，`outcome=v`一定对该线程可见。

``````private int awaitDone(boolean timed, long nanos) {
xxxx

int s = state;
if (s > COMPLETING) {
if (q != null)
return s;
}
else if (s == COMPLETING) // cannot time out yet