1. 程式人生 > >多執行緒中的Runnable、Callable、Future、FutureTask的作用

多執行緒中的Runnable、Callable、Future、FutureTask的作用

在Java多執行緒程式設計中,建立啟動一個執行緒可以繼承Thread類,也可以實現Runnable介面,但是想要獲取執行緒的結果,則需要實現Callable介面,獲取結果則需要使用Future介面。下面分別介紹這幾個介面或者類的實現。

Runnable介面

Runnable介面只有一個run方法,其執行緒執行邏輯在run方法中實現。但是該方法沒有返回值,所以不能獲得執行緒執行的結果。其介面定義如下:

  1. publicinterface Runnable {  
  2.     /** 
  3.      * When an object implementing interface <code>Runnable</code> is used
     
  4.      * to create a thread, starting the thread causes the object's 
  5.      * <code>run</code> method to be called in that separately executing 
  6.      * thread. 
  7.      * <p> 
  8.      * 
  9.      * @see     java.lang.Thread#run() 
  10.      */
  11.     publicabstractvoid run();  
  12. }  

Callable介面

Callable介面和Runnable介面很相似,但是其call方法有返回值,所以可以獲取執行緒執行的返回值。

  1. publicinterface Callable<V> {  
  2.     /** 
  3.      * Computes a result, or throws an exception if unable to do so. 
  4.      * 
  5.      * @return computed result 
  6.      * @throws Exception if unable to compute a result 
  7.      */
  8.     V call() throws Exception;  
  9. }  

Future介面

Future就是對於具體的Runnable或者Callable任務的執行結果進行

取消、查詢是否完成、獲取結果、設定結果操作。其get方法會阻塞,

直到超時,或者獲得任務執行結果。

  1. /** 
  2. * @see FutureTask 
  3.  * @see Executor 
  4.  * @since 1.5 
  5.  * @author Doug Lea 
  6.  * @param <V> The result type returned by this Future's <tt>get</tt> method 
  7.  */
  8. publicinterface Future<V> {  
  9.     /** 
  10.      * Attempts to cancel execution of this task.  This attempt will 
  11.      * fail if the task has already completed, has already been cancelled, 
  12.      * or could not be cancelled for some other reason. If successful, 
  13.      * and this task has not started when <tt>cancel</tt> is called, 
  14.      * this task should never run.  If the task has already started, 
  15.      * then the <tt>mayInterruptIfRunning</tt> parameter determines 
  16.      * whether the thread executing this task should be interrupted in 
  17.      * an attempt to stop the task.     * 
  18.      */
  19.     boolean cancel(boolean mayInterruptIfRunning);  
  20.     /** 
  21.      * Returns <tt>true</tt> if this task was cancelled before it completed 
  22.      * normally. 
  23.      */
  24.     boolean isCancelled();  
  25.     /** 
  26.      * Returns <tt>true</tt> if this task completed. 
  27.      * 
  28.      */
  29.     boolean isDone();  
  30.     /** 
  31.      * Waits if necessary for the computation to complete, and then 
  32.      * retrieves its result. 
  33.      * 
  34.      * @return the computed result 
  35.      */
  36.     V get() throws InterruptedException, ExecutionException;  
  37.     /** 
  38.      * Waits if necessary for at most the given time for the computation 
  39.      * to complete, and then retrieves its result, if available. 
  40.      * 
  41.      * @param timeout the maximum time to wait 
  42.      * @param unit the time unit of the timeout argument 
  43.      * @return the computed result 
  44.      */
  45.     V get(long timeout, TimeUnit unit)  
  46.         throws InterruptedException, ExecutionException, TimeoutException;  

FutureTask

FutureTask是一個RunnableFuture<V>介面實現,而RunnableFuture<V>實現了Runnable和Future兩個介面,同時,它還可以包裝

Runnable和Callable介面,由構造函式注入:

  1. public FutureTask(Callable<V> callable) {  
  2.     if (callable == null)  
  3.         thrownew NullPointerException();  
  4.     this.callable = callable;  
  5.     this.state = NEW;       // ensure visibility of callable
  6. }  
  7. public FutureTask(Runnable runnable, V result) {  
  8.     this.callable = Executors.callable(runnable, result);  
  9.     this.state = NEW;       // ensure visibility of callable
  10. }  

由於FutureTask實現了Runnable介面和Future介面,同時包裝了Callable,所以它既可以傳遞給Thread類來執行,也可以提交給

ExecuteService來執行。並且還可以直接通過get函式來獲取執行結果。