多線程-Thread,Runnable,Callable,Future,RunnableFuture,FutureTask

來源:互聯網
上載者:User

標籤:ace   out   pos   logs   stat   owa   線程   調用   es2017   

類圖:

 

先看各自的源碼:

public interface Runnable {    public abstract void run();}

  

publicclass Thread implements Runnable {    /* What will be run. */    private Runnable target;}

Thread與Runnable其實是一個裝飾器模式。

public interface Callable<V> {    V call() throws Exception;}

  

public interface Future<V> {    boolean cancel(boolean mayInterruptIfRunning);    boolean isCancelled();    boolean isDone();    V get() throws InterruptedException, ExecutionException;    V get(long timeout, TimeUnit unit)        throws InterruptedException, ExecutionException, TimeoutException;}

  

public interface RunnableFuture<V> extends Runnable, Future<V> {    void run();}

  

public class FutureTask<V> implements RunnableFuture<V> { /** The underlying callable; nulled out after running */    private Callable<V> callable; /** The result to return or exception to throw from get() */    private Object outcome;    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 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);        }    }}

從類的結構來看: 
Runnable,Callable,Future介面本是互相獨立的,沒有直接的關係。 
而Thread的一系列建構函式需要的是Runnable對象,所以Callable對象並不適合Thread建構函式,而是藉助於FutureTask這個類,該類實現具有Future和Runnable介面能力的類,同時組合Callable對象,FutureTask在run()方法中調用Callable的call()方法,這是個典型的適配器模式: 
Runable是個目標介面,定義了Thread所需的介面; 
Callable是被適配介面,定義了一個已經存在的介面方法call() 
FutureTask是個適配器類,作為一個轉換器,將Runnable和Callable適配,適配器類是適配器模式的核心,它通過實現RunnableFuture和組合Callable使得兩者產生關係。

多線程-Thread,Runnable,Callable,Future,RunnableFuture,FutureTask

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.