分析 Java 線程池 Callable 任務執行原理

KariShaffer 8年前發布 | 15K 次閱讀 線程池 Java Java開發

上一篇分析了線程池的執行原理,主要關于線程池的生命周期和任務如何在池里創建、運行和終止。不過上次研究的是execute方法,執行的是Runnable任務,它不返回任何值。如果希望任務完成后返回結果,那么需要使用Callable接口,這也是本文要研究的主題。

ExecutorService es = Executors.newSingleThreadExecutor();
Future<?> task = es.submit(new MyThread());
try {
    //限定時間獲取結果
    task.get(5, TimeUnit.SECONDS);
} catch (TimeoutException e) {
    //超時觸發線程中止
    System.out.println("thread over time");
} catch (ExecutionException e) {
   //拋出執行異常
    throw e;
} finally {
   //如果任務還在運行,執行中斷
    boolean mayInterruptIfRunning = true;
    task.cancel(mayInterruptIfRunning);
}

上面代碼是Future的一個簡單例子:MyThread實現Callable接口,執行時要求在限定時間內獲取結果,超時執行會拋出TimeoutException,執行異常會拋出ExecutionException。最后在finally里,如果任務還在執行,就進行取消;如果任務已經執行完,取消操作也沒有影響。

圖1 FutureTask

Future接口代表一個異步任務的結果,提供了相應方法判斷任務是否完成或者取消。從圖1可知,RunnableFuture同時繼承了Future和Runnable,是一個可運行、可知結果的任務,FutureTask是具體的實現類。

FutureTask的狀態

private volatile int state;
private static final int NEW          = 0;
private static final int COMPLETING   = 1;
private static final int NORMAL       = 2;
private static final int EXCEPTIONAL  = 3;
private static final int CANCELLED    = 4;
private static final int INTERRUPTING = 5;
private static final int INTERRUPTED  = 6;

FutureTask有7種狀態,初始狀態從NEW開始,狀態轉換路徑可以歸納為圖2所示。在后文的代碼,會使用int的大小比較判斷狀態處于哪個范圍,需要留意上面狀態的排列順序。

圖2 FutureTask狀態路徑

FutureTask的狀態路徑,取決于run和cancel的調用順序,在后文分析時,對號入座這幾條路徑。

  1. NEW -> COMPLETING -> NORMAL 正常的流程
  2. NEW -> COMPLETING -> EXCEPTIONAL 異常的流程
  3. NEW -> CANCELLED 被取消流程
  4. NEW -> INTERRUPTING -> INTERRUPTED 被中斷流程

FutureTask的變量

  • int state
  • Thread runner
  • WaitNode waiters
  • Callable<V> callable
  • Object outcome

state、runner、waiters三個變量沒有使用原子類,而是使用Unsafe對象進行原子操作。代碼中會見到很多形如compareAndSwap的方法,入門原理可以看我以前寫的 認識非阻塞的同步機制CAS 。

callable是要執行的任務,runner是執行任務的線程,outcome是返回的結果(正常結果或Exception結果)

static final class WaitNode {
    volatile Thread thread;
    volatile WaitNode next;
    WaitNode() { thread = Thread.currentThread(); }
}

waiters的數據結構是WaitNode,保存了Thread和下個WaitNode的引用。waiters保存了等待結果的線程,每次操作只會增減頭,所以是一個棧結構,詳細見后文對get方法的分析。

FutureTask的創建

public FutureTask(Callable<V> callable) {
    if (callable == null)
        throw new NullPointerException();
    this.callable = callable;
    this.state = NEW;       // ensure visibility of callable
}

public FutureTask(Runnable runnable, V result) {
    this.callable = Executors.callable(runnable, result);
    this.state = NEW;       // ensure visibility of callable
}

FutureTask可以接受Callable或者Runnable,state從NEW開始。如果是Runnable,需要調用Executors.callable轉成Callable,返回的結果是預先傳入的result。轉換過程使用一個實現了Callable的RunnableAdapter包裝Runnable和result,代碼比較簡單。

static final class RunnableAdapter<T> implements Callable<T> {
    final Runnable task;
    final T result;
    RunnableAdapter(Runnable task, T result) {
        this.task = task;
        this.result = result;
    }
    public T call() {
        task.run();
        return result;
    }
}

提交FutureTask到線程池的submit定義在AbstractExecutorService,根據入參的不同,有三個submit方法。下面以提交Callable為例:

public <T> Future<T> submit(Callable<T> task) {
   if (task == null) throw new NullPointerException();
   RunnableFuture<T> ftask = newTaskFor(task);
   execute(ftask);
   return ftask;
}

protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {    
   return new FutureTask<T>(callable);
}

FutureTask在newTaskFor創建,然后調用線程池的execute執行,最后返回Future。獲取Future后,就可以調用get獲取結果,或者調用cancel取消任務。

FutureTask的運行

FutureTask實現了Runnable,在線程池里執行時調用的方法是run。

public void run() {
    //1
    if (state != NEW ||
        !UNSAFE.compareAndSwapObject(this, runnerOffset,null, Thread.currentThread()))
        return;
    //2
    try {
        Callable<V> c = callable;
        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 {
       //3
        runner = null;
        int s = state;
        if (s >= INTERRUPTING)
            handlePossibleCancellationInterrupt(s);
    }
}

標記1處檢查FutureTask的狀態,如果不是處于NEW,說明狀態已經進入四條路徑之一,也就沒有必要繼續了。如果狀態是NEW,則將執行任務的線程交給runner。

標記2處開始正式執行任務,調用call方法獲取結果,沒有異常就算成功,最后執行set方法;出現異常就調用setException方法。

標記3處,無論任務執行是否成功,都需要將runner重新置為空。

protected void set(V v) {
    if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
        outcome = v;
        UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
        finishCompletion();
    }
}

protected void setException(Throwable t) {
    if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
        outcome = t;
        UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state
        finishCompletion();
    }
}

任務執行成功與失敗,分別對應NEW -> COMPLETING -> NORMAL和NEW -> COMPLETING -> EXCEPTIONAL兩條路徑。這里先將狀態修改為中間狀態,再對結果賦值,最后再修改為最終狀態。

private void finishCompletion() {
    // assert state > COMPLETING;
    for (WaitNode q; (q = waiters) != null;) {
        if (UNSAFE.compareAndSwapObject(this, waitersOffset, 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;
        }
    }
    done();
    callable = null;        // to reduce footprint
}

最后調用finishCompletion執行任務完成,喚醒并刪除所有在waiters中等待的線程。done方法是空的,供子類實現,最后callable也設置為空。

FutureTask還有個runAndReset,邏輯和run類似,但沒有調用set方法來設置結果,執行完成后將任務重新初始化。

protected boolean runAndReset() {
    if (state != NEW ||
        !UNSAFE.compareAndSwapObject(this, runnerOffset,
                                     null, Thread.currentThread()))
        return false;
    boolean ran = false;
    int s = state;
    try {
        Callable<V> c = callable;
        if (c != null && s == NEW) {
            try {
                c.call(); // don't set result
                ran = true;
            } catch (Throwable ex) {
                setException(ex);
            }
        }
    } 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
        s = state;
        if (s >= INTERRUPTING)
            handlePossibleCancellationInterrupt(s);
    }
    return ran && s == NEW;
}

FutureTask的取消

對于已經提交執行的任務,可以調用cancel執行取消。

public boolean cancel(boolean mayInterruptIfRunning) {
   //1
    if (!(state == NEW &&
          UNSAFE.compareAndSwapInt(this, stateOffset, NEW,
              mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
        return false;
    try {    // in case call to interrupt throws exception
       //2
        if (mayInterruptIfRunning) {
            try {
                Thread t = runner;
                if (t != null)
                    t.interrupt();
            } finally { // final state
                UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);
            }
        }
    } finally {
        finishCompletion();
    }
    return true;
}

標記1處判斷任務狀態,為NEW才能被取消。如果mayInterruptIfRunning是true,代表任務需要被中斷,走NEW -> INTERRUPTING -> INTERRUPTED流程。否則代表任務被取消,走NEW -> CANCELLED流程。

標記2處理任務被中斷的情況,這里僅僅是對線程發出中斷請求,不確保任務能檢測并處理中斷,詳細原理去看Java的中斷機制。

最后調用finishCompletion完成收尾工作。

public boolean isCancelled() {
    return state >= CANCELLED;
}

判斷任務是否被取消,具體邏輯是判斷state >= CANCELLED,包括了被中斷一共兩條路徑的結果。

FutureTask獲取結果

調用FutureTask的get方法獲取任務的執行結果,可以阻塞直到獲取結果,也可以限制范圍時間內獲取結果,否則拋出TimeoutException。

public V get() throws InterruptedException, ExecutionException {
    int s = state;
    if (s <= COMPLETING)
        s = awaitDone(false, 0L);
    return report(s);
}

public V get(long timeout, TimeUnit unit)
    throws InterruptedException, ExecutionException, TimeoutException {
    if (unit == null)
        throw new NullPointerException();
    int s = state;
    if (s <= COMPLETING &&
        (s = awaitDone(true, unit.toNanos(timeout))) <= COMPLETING)
        throw new TimeoutException();
    return report(s);
}

get的核心實現調用了awaitDone,入參為是否開啟時間限制和最大的等待時間。

private int awaitDone(boolean timed, long nanos)
    throws InterruptedException {
    final long deadline = timed ? System.nanoTime() + nanos : 0L;
    WaitNode q = null;
    boolean queued = false;
    for (;;) {
        if (Thread.interrupted()) {
            removeWaiter(q);
            throw new InterruptedException();
        }

        int s = state;
        if (s > COMPLETING) {    //1
            if (q != null)
                q.thread = null;
            return s;
        }
        else if (s == COMPLETING) // cannot time out yet    //2
            Thread.yield();
        else if (q == null)     //3
            q = new WaitNode();
        else if (!queued)    //4
            queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
                                                 q.next = waiters, q);
        else if (timed) {    //5
            nanos = deadline - System.nanoTime();
            if (nanos <= 0L) {
                removeWaiter(q);
                return state;
            }
            LockSupport.parkNanos(this, nanos);
        }
        else     //6
            LockSupport.park(this);
    }
}

awaitDone主要邏輯是一個無限循環,首先判斷線程是否被中斷,是的話移除waiter并拋出中斷異常。接下來是一串if-else,一共六種情況。

  1. 判斷任務狀態是否已經完成,是就直接返回;
  2. 任務狀態是COMPLETING,代表在set結果時被阻塞了,這里先讓出資源;
  3. 如果WaitNode為空,就為當前線程初始化一個WaitNode;
  4. 如果當前的WaitNode還沒有加入waiters,就加入;
  5. 如果是限定時間執行,判斷有無超時,超時就將waiter移出,并返回結果,否則阻塞一定時間;
  6. 如果沒有限定時間,就一直阻塞到下次被喚醒。

LockSupport是用來創建鎖和其他同步類的基本線程阻塞原語。park和unpark的作用分別是阻塞線程和解除阻塞線程。

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

最后get調用report,使用outcome返回結果。

圖3

看圖3,如果多個線程向同一個FutureTask實例get結果,但FutureTask又沒有執行完畢,線程將會阻塞并保存在waiters中。待FutureTask獲取結果后,喚醒waiters等待的線程,并返回同一個結果。

總結

圖4

圖4歸納了FutureTask的作用,任務的調用線程Caller和線程池的工作線程通過FutureTask交互。對比線程池的執行原理,FutureTask是比較簡單的。

 

來自:http://www.jianshu.com/p/f624934b9a23

 

 本文由用戶 KariShaffer 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!