您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Java異步任務(wù)計(jì)算FutureTask源碼分析”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Java異步任務(wù)計(jì)算FutureTask源碼分析”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
FutureTask 是一個(gè)可取消的異步計(jì)算。
FutureTask
提供了對(duì)Future的基本實(shí)現(xiàn),可以調(diào)用方法去開始和取消一個(gè)計(jì)算,可以查詢計(jì)算是否完成,并且獲取計(jì)算結(jié)果。
FutureTask只能在計(jì)算完成后獲取到計(jì)算結(jié)果,一旦計(jì)算完成,將不能重啟或者取消,除非調(diào)用runAndReset方法。
FutureTask除了實(shí)現(xiàn)了Future接口以外,還實(shí)現(xiàn)了Runnable
接口,因此FutureTask是可以交由線程池的Executor執(zhí)行,也可以直接使用一個(gè)異步線程調(diào)用執(zhí)行(futureTask.run())。
首先,我們看一下FutureTask
類的繼承結(jié)構(gòu),如下圖,它實(shí)現(xiàn)的是RunnableFuture
接口,而RunnableFuture
繼承自Future和函數(shù)式接口Runnable
,所以說FutureTask本質(zhì)就是一個(gè)可運(yùn)行的Future。
Future 接口約定了一些異步計(jì)算類必須要實(shí)現(xiàn)的功能,源碼如下:
package java.util.concurrent; public interface Future<V> { /** * 嘗試取消任務(wù)的執(zhí)行,并返回取消結(jié)果。 * 參數(shù)mayInterruptIfRunning:是否中斷線程。 */ boolean cancel(boolean mayInterruptIfRunning); /** * 判斷任務(wù)是否被取消(正常結(jié)束之前被被取消返回true) */ boolean isCancelled(); /** * 判斷當(dāng)前任務(wù)是否執(zhí)行完畢,包括正常執(zhí)行完畢、執(zhí)行異?;蛘呷蝿?wù)取消。 */ boolean isDone(); /** * 獲取任務(wù)執(zhí)行結(jié)果,任務(wù)結(jié)束之前會(huì)阻塞。 */ V get() throws InterruptedException, ExecutionException; /** * 在指定時(shí)間內(nèi)嘗試獲取執(zhí)行結(jié)果。若超時(shí)則拋出超時(shí)異常TimeoutException */ V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
Runnable
接口我們都很熟悉,他就是一個(gè)函數(shù)式接口,我們常用其創(chuàng)建一個(gè)線程。
package java.lang; ? @FunctionalInterface public interface Runnable { ? ? ? ?public abstract void run(); }
FutureTask就是一個(gè)將要被執(zhí)行的任務(wù),它包含了以上接口具體的實(shí)現(xiàn),F(xiàn)utureTask內(nèi)部定義了任務(wù)的狀態(tài)state和一些狀態(tài)的常量,它的內(nèi)部核心是一個(gè)Callable callable,我們通過構(gòu)造函數(shù)可以傳入callable或者是runnable,最后都會(huì)內(nèi)部轉(zhuǎn)為callable,因?yàn)槲覀冃枰@取異步任務(wù)的執(zhí)行結(jié)果,只有通過Callable創(chuàng)建的線程才會(huì)返回結(jié)果。
我們可以通過此時(shí)的狀態(tài)判斷Future中isCancelled()
,isDone()
的返回結(jié)果。
以下為FutureTask源碼,內(nèi)含核心源碼分析注釋
package java.util.concurrent; import java.util.concurrent.locks.LockSupport; public class FutureTask<V> implements RunnableFuture<V> { /** * 任務(wù)的運(yùn)行狀態(tài) */ 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; // 中斷的 private Callable<V> callable; /** * 返回結(jié)果 */ private Object outcome; private volatile Thread runner; private volatile WaitNode waiters; ... public FutureTask(Callable<V> callable) { if (callable == null) throw new NullPointerException(); this.callable = callable; this.state = NEW; } public FutureTask(Runnable runnable, V result) { this.callable = Executors.callable(runnable, result); this.state = NEW; } public boolean isCancelled() { return state >= CANCELLED; } public boolean isDone() { return state != NEW; } /* * 取消任務(wù)實(shí)現(xiàn) * 如果任務(wù)還沒有啟動(dòng)就調(diào)用了cancel(true),任務(wù)將永遠(yuǎn)不會(huì)被執(zhí)行。 * 如果任務(wù)已經(jīng)啟動(dòng),參數(shù)mayInterruptIfRunning將決定任務(wù)是否應(yīng)該中斷執(zhí)行該任務(wù)的線程,以嘗試中斷該任務(wù)。 * 如果任務(wù)任務(wù)已經(jīng)取消、已經(jīng)完成或者其他原因不能取消,嘗試將失敗。 */ public boolean cancel(boolean mayInterruptIfRunning) { if (!(state == NEW && UNSAFE.compareAndSwapInt(this, stateOffset, 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 UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED); } } } finally { finishCompletion(); } return true; } /* * 等待獲取結(jié)果 * 獲取當(dāng)前狀態(tài),判斷是否執(zhí)行完成。并且判斷時(shí)間是否超時(shí) * 如果任務(wù)沒有執(zhí)行完成,就阻塞等待完成,若超時(shí)拋出超時(shí)等待異常。 */ public V get() throws InterruptedException, ExecutionException { int s = state; if (s <= COMPLETING) s = awaitDone(false, 0L); return report(s); } /* * 等待獲取結(jié)果 * 獲取當(dāng)前狀態(tài),判斷是否執(zhí)行完成。 * 如果任務(wù)沒有執(zhí)行完成,就阻塞等待完成。 */ 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); } /** * 根據(jù)狀態(tài)判斷返回結(jié)果還是異常 */ 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); } protected void done() { } /** * 設(shè)置結(jié)果借助CAS確認(rèn)狀態(tài)是否完成狀態(tài) */ protected void set(V v) { if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) { outcome = v; UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state finishCompletion(); } } /** * 設(shè)置異常,當(dāng)運(yùn)行完成出現(xiàn)異常,設(shè)置異常狀態(tài) */ protected void setException(Throwable t) { if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) { outcome = t; UNSAFE.putOrderedInt(this, stateOffset, EXCEPTIONAL); // final state finishCompletion(); } } /* * 執(zhí)行callable獲取結(jié)果,或者異常 * 判斷狀態(tài)是不是啟動(dòng)過的,如果是新建才可以執(zhí)行run方法 */ public void run() { if (state != NEW || !UNSAFE.compareAndSwapObject(this, runnerOffset, null, Thread.currentThread())) return; 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 { runner = null; int s = state; if (s >= INTERRUPTING) handlePossibleCancellationInterrupt(s); } } /** * 重新執(zhí)行 */ 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 = null; s = state; if (s >= INTERRUPTING) handlePossibleCancellationInterrupt(s); } return ran && s == NEW; } /* * 處理可能取消的中斷 */ private void handlePossibleCancellationInterrupt(int s) { if (s == INTERRUPTING) while (state == INTERRUPTING) Thread.yield(); } static final class WaitNode { volatile Thread thread; volatile WaitNode next; WaitNode() { thread = Thread.currentThread(); } } /** * 移除并喚醒所有等待線程,執(zhí)行done,置空callable */ 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 } /** * 等待完成 * 首先判斷是否超時(shí) * 處理中斷的,然后處理異常狀態(tài)的,處理完成的... */ 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) { if (q != null) q.thread = null; return s; } else if (s == COMPLETING) // cannot time out yet Thread.yield(); else if (q == null) q = new WaitNode(); else if (!queued) queued = UNSAFE.compareAndSwapObject(this, waitersOffset, q.next = waiters, q); else if (timed) { nanos = deadline - System.nanoTime(); if (nanos <= 0L) { removeWaiter(q); return state; } LockSupport.parkNanos(this, nanos); } else LockSupport.park(this); } } /** * 去除等待 */ private void removeWaiter(WaitNode node) { if (node != null) { node.thread = null; retry: for (;;) { // restart on removeWaiter race for (WaitNode pred = null, q = waiters, s; q != null; q = s) { s = q.next; if (q.thread != null) pred = q; else if (pred != null) { pred.next = s; if (pred.thread == null) // check for race continue retry; } else if (!UNSAFE.compareAndSwapObject(this, waitersOffset, q, s)) continue retry; } break; } } } // Unsafe mechanics private static final sun.misc.Unsafe UNSAFE; private static final long stateOffset; private static final long runnerOffset; private static final long waitersOffset; static { try { UNSAFE = sun.misc.Unsafe.getUnsafe(); Class<?> k = FutureTask.class; stateOffset = UNSAFE.objectFieldOffset (k.getDeclaredField("state")); runnerOffset = UNSAFE.objectFieldOffset (k.getDeclaredField("runner")); waitersOffset = UNSAFE.objectFieldOffset (k.getDeclaredField("waiters")); } catch (Exception e) { throw new Error(e); } } }
一般來說,我們可以認(rèn)為FutureTask具有以下三種狀態(tài):
未啟動(dòng):新建的FutureTask,在run()沒執(zhí)行之前,F(xiàn)utureTask處于未啟動(dòng)狀態(tài)。
private static final int NEW = 0; // 新建
已啟動(dòng):FutureTask
對(duì)象的run方法啟動(dòng)并執(zhí)行的過程中,F(xiàn)utureTask處于已啟動(dòng)狀態(tài)。
已完成:FutureTask正常執(zhí)行結(jié)束,或者FutureTask
執(zhí)行被取消(FutureTask對(duì)象cancel方法),或者FutureTask對(duì)象run方法執(zhí)行拋出異常而導(dǎo)致中斷而結(jié)束,F(xiàn)utureTask都處于已完成狀態(tài)。
private static final int COMPLETING = 1; // 完成 private static final int NORMAL = 2; // 完成后正常設(shè)置結(jié)果 private static final int EXCEPTIONAL = 3; // 完成后異常設(shè)置異常 private static final int CANCELLED = 4; // 執(zhí)行取消 private static final int INTERRUPTING = 5; // 中斷中 private static final int INTERRUPTED = 6; // 中斷的
使用一(直接新建一個(gè)線程調(diào)用):
FutureTask<Integer> task = new FutureTask<>(new Callable() { @Override public Integer call() throws Exception { return sum(); } }); new Thread(task).stat(); Integer result = task.get();
使用二(結(jié)合線程池使用)
FutureTask<Integer> task = new FutureTask<>(new Callable() { @Override public Integer call() throws Exception { return sum(); } }); Executors.newCachedThreadPool().submit(task); Integer result = task.get();
讀到這里,這篇“Java異步任務(wù)計(jì)算FutureTask源碼分析”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。