国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

多線程相關 Thread、Runnable、Callable、Futrue類關系與區(qū)別

2019-11-09 14:30:58
字體:
來源:轉載
供稿:網(wǎng)友
java中存在Runnable、Callable、Future、FutureTask這幾個與線程相關的類或者接口,在Java中也是比較重要的幾個概念,我們通過下面的簡單示例來了解一下它們的作用于區(qū)別。

Runnable

其中Runnable應該是我們最熟悉的接口,它只有一個run()函數(shù),用于將耗時操作寫在其中,該函數(shù)沒有返回值。然后使用某個線程去執(zhí)行該runnable即可實現(xiàn)多線程,Thread類在調用start()函數(shù)后就是執(zhí)行的是Runnable的run()函數(shù)。Runnable的聲明如下 : 

[java] view plain copy在CODE上查看代碼片public interface Runnable {      /**      * When an object implementing interface <code>Runnable</code> is used      * to create a thread, starting the thread causes the object's      * <code>run</code> method to be called in that separately executing      * thread.      * <p>      *      * @see     java.lang.Thread#run()      */      public abstract void run();  }  

Callable

Callable與Runnable的功能大致相似,Callable中有一個call()函數(shù),但是call()函數(shù)有返回值,而Runnable的run()函數(shù)不能將結果返回給客戶程序。Callable的聲明如下 :

[java] view plain copy在CODE上查看代碼片public interface Callable<V> {      /**      * Computes a result, or throws an exception if unable to do so.      *      * @return computed result      * @throws Exception if unable to compute a result      */      V call() throws Exception;  }  可以看到,這是一個泛型接口,call()函數(shù)返回的類型就是客戶程序傳遞進來的V類型。

Future

Executor就是Runnable和Callable的調度容器,Future就是對于具體的Runnable或者Callable任務的執(zhí)行結果進行

取消、查詢是否完成、獲取結果、設置結果操作。get方法會阻塞,直到任務返回結果(Future簡介)。Future聲明如下 :

[java] view plain copy在CODE上查看代碼片/** * @see FutureTask  * @see Executor  * @since 1.5  * @author Doug Lea  * @param <V> The result type returned by this Future's <tt>get</tt> method  */  public interface Future<V> {        /**      * Attempts to cancel execution of this task.  This attempt will      * fail if the task has already completed, has already been cancelled,      * or could not be cancelled for some other reason. If successful,      * and this task has not started when <tt>cancel</tt> is called,      * this task should never run.  If the task has already started,      * then the <tt>mayInterruptIfRunning</tt> parameter determines      * whether the thread executing this task should be interrupted in      * an attempt to stop the task.     *      */      boolean cancel(boolean mayInterruptIfRunning);        /**      * Returns <tt>true</tt> if this task was cancelled before it completed      * normally.      */      boolean isCancelled();        /**      * Returns <tt>true</tt> if this task completed.      *      */      boolean isDone();        /**      * Waits if necessary for the computation to complete, and then      * retrieves its result.      *      * @return the computed result      */      V get() throws InterruptedException, ExecutionException;        /**      * Waits if necessary for at most the given time for the computation      * to complete, and then retrieves its result, if available.      *      * @param timeout the maximum time to wait      * @param unit the time unit of the timeout argument      * @return the computed result      */      V get(long timeout, TimeUnit unit)          throws InterruptedException, ExecutionException, TimeoutException;  }  

FutureTask

FutureTask則是一個RunnableFuture<V>,而RunnableFuture實現(xiàn)了Runnbale又實現(xiàn)了Futrue<V>這兩個接口,

[java] view plain copy在CODE上查看代碼片public class FutureTask<V> implements RunnableFuture<V>  RunnableFuture

[java] view plain copy在CODE上查看代碼片public interface RunnableFuture<V> extends Runnable, Future<V> {      /**      * Sets this Future to the result of its computation      * unless it has been cancelled.      */      void run();  }  

另外它還可以包裝Runnable和Callable<V>, 由構造函數(shù)注入依賴。

[java] view plain copy在CODE上查看代碼片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  }  可以看到,Runnable注入會被Executors.callable()函數(shù)轉換為Callable類型,即FutureTask最終都是執(zhí)行Callable類型的任務。該適配函數(shù)的實現(xiàn)如下 :

[java] view plain copy在CODE上查看代碼片public static <T> Callable<T> callable(Runnable task, T result) {      if (task == null)          throw new NullPointerException();      return new RunnableAdapter<T>(task, result);  }  RunnableAdapter適配器

[java] view plain copy在CODE上查看代碼片/**  * A callable that runs given task and returns given 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實現(xiàn)了Runnable,因此它既可以通過Thread包裝來直接執(zhí)行,也可以提交給ExecuteService來執(zhí)行。

并且還可以直接通過get()函數(shù)獲取執(zhí)行結果,該函數(shù)會阻塞,直到結果返回。因此FutureTask既是Future、

Runnable,又是包裝了Callable( 如果是Runnable最終也會被轉換為Callable ), 它是這兩者的合體。

簡單示例

[java] view plain copy在CODE上查看代碼片 package com.effective.java.concurrent.task;    import java.util.concurrent.Callable;  import java.util.concurrent.ExecutionException;  import java.util.concurrent.ExecutorService;  import java.util.concurrent.Executors;  import java.util.concurrent.Future;  import java.util.concurrent.FutureTask;    /**  *   * @author mrsimple  *  */  public class RunnableFutureTask {        /**      * ExecutorService      */      static ExecutorService mExecutor = Executors.newSingleThreadExecutor();        /**      *       * @param args      */      public static void main(String[] args) {          runnableDemo();          futureDemo();      }        /**      * runnable, 無返回值      */      static void runnableDemo() {            new Thread(new Runnable() {                @Override              public void run() {                  System.out.

輸出結果

Thread、Runnable、Callable,其中Runnable實現(xiàn)的是void run()方法,Callable實現(xiàn)的是 V call()方法,并且可以返回執(zhí)行結果,其中Runnable可以提交給Thread來包裝下,直接啟動一個線程來執(zhí)行,而Callable則一般都是提交給ExecuteService來執(zhí)行。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 黔南| 若羌县| 鹿泉市| 泸定县| 洪雅县| 桃园县| 元朗区| 中宁县| 禹州市| 栾川县| 阿鲁科尔沁旗| 峨边| 新干县| 涞源县| 新昌县| 拜泉县| 靖江市| 南川市| 新宁县| 弋阳县| 诸城市| 紫阳县| 平原县| 天津市| 嘉善县| 岳阳市| 临夏市| 霞浦县| 那坡县| 永靖县| 禄丰县| 桃园县| 兴文县| 义马市| 南开区| 同江市| 洪雅县| 衡水市| 丰都县| 神池县| 鲁山县|