FutureTask註釋
FutureTask屬性
NORMAL 正常 2
EXCEPTIONAL 異常 3
CANCELLED 取消 4
INTERRUPTING 中斷中 5
INTERRUNPED 被中斷 6
NEW->CANCELED 被取消
NEW->INTERRUNPING->INTERRRUNPTED 被中斷
public FutureTask(Runnable runnable, V result) {
this.callable = Executors.callable(runnable, result);
this.state = NEW; // ensure visibility of callable
}
FutureTask run()
public void run() {
1、
首先判斷任務的狀態 如果任務的狀態不是new 說明任務的狀態已經改變(說明他已經走了4種可能變化的一種)- 2、如果狀態是new就會把 當前執行任務的線程付給runner, 這裏用的cmpandset如果runner不爲空 說明已經有線程在執行
- 3、任務也會退出執行,如果狀態是new並且runner爲空並且把當前的線程付給了runner那麼就繼續執行任務(runner state 都是 volatile 類型的變量是一個很輕量機的線程安全操作)
- 4、引起state狀態變化的原因 就是調用了cancel 或是 run
if (state != NEW ||
!U.compareAndSwapObject(this, RUNNER, null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
//
如果 要執行的任務不爲空 並且狀態 new 就執行if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
protected void set(V v) {
//
如果state是new 把state設置成 COMPLETINGif (U.compareAndSwapInt(this, STATE, NEW, COMPLETING)) {
// 將結果保存到outcome
outcome = v;
U.putOrderedInt(this, STATE, NORMAL); // final state
finishCompletion();
}
}
protected void setException(Throwable t) {
//
如果state是new 把state設置成 COMPLETINGif (U.compareAndSwapInt(this, STATE, NEW, COMPLETING)) {
outcome = t;
U.putOrderedInt(this, STATE, EXCEPTIONAL); // final state
finishCompletion();
}
}
FutureTask cancel()
public boolean cancel(boolean mayInterruptIfRunning) {
// mayInterruptIfRunning 是否中斷running
// 這個判斷邏輯等價於:
//
if(state!=new || !UNSAFE.compareAndSwapInt(this, stateOffset, NEW, mayInterruptIfRunning ? INTERRUPTING : CANCELLED))- // 1、如果state不是new 那麼就退出方法,這時的任務是已經完成了 或是被取消了 或是被中斷了
- // 2、如果是state是new 就設置state 爲中斷狀態 或是取消狀態
if (!(state == NEW &&
U.compareAndSwapInt(this, STATE, NEW,
mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
return false;
try { // in case call to interrupt throws exception
if (mayInterruptIfRunning) {
try {
Thread t = runner;
if (t != null)
t.interrupt();
} finally { // final state
U.putOrderedInt(this, STATE, INTERRUPTED);
}
}
} finally {
finishCompletion();
}
return true;
}
- 如果Task已經在執行而callable.call()沒有返回 或是 call()已經返回但是state狀態還沒有改變,那麼任務調用cancel(false) 不會對任務的執行造成影響 只會影響task的狀態 。
- 如果callable.call()已經返回並且狀態已經變成COMPLETING或是 COMPLED 那麼對任務執行 和任務狀態都沒有影響。
- 如果任務已經在執行而callable.call()沒有返回,會把state設置成 INTERRUPTING然後調用執行線程的中斷請求 然後把狀態設置成INTERRUPTED,這裏如果 callable.call()方法可以響應中斷 可能對任務執行產生影響,如果方法不會響應中斷不會對任務運行產生影響。影響任務的狀態。
- 如果任務已經在執行並且 call()已經返回但是state狀態還沒有改變 不會對任務的執行造成影響 只會影響任務的狀態 。
任務結果回調方法 done()
private void finishCompletion() {
// assert state > COMPLETING;
for (WaitNode q; (q = waiters) != null;) {
if (U.compareAndSwapObject(this, WAITERS, q, null)) {
for (;;) {
Thread t = q.thread;
if (t != null) {
q.thread = null;
LockSupport.unpark(t);
}
WaitNode next = q.next;
if (next == null)
break;
q.next = null; // unlink to help gc
q = next;
}
break;
}
}
- // 子類覆寫此方法,在計算結果結束後會回調此方法,獲取結果的方法:get()
done();
callable = null; // to reduce footprint
}
獲取結果的方法 get()
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;
// 如果是異常,則拋出異常
CancellationExceptionif (s >= CANCELLED)
throw new CancellationException();
throw new ExecutionException((Throwable)x);
}