日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java中的Runnable、Callable、Future、FutureTask

發布時間:2025/3/18 java 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java中的Runnable、Callable、Future、FutureTask 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java中存在Runnable、Callable、Future、FutureTask這幾個與線程相關的類或者接口,在Java中也是比較重要的幾個概念,我們通過下面的簡單示例來了解一下它們的作用于區別。

Runnable

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

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()函數,但是call()函數有返回值,而Runnable的run()函數不能將結果返回給客戶程序。Callable的聲明如下 :

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()函數返回的類型就是客戶程序傳遞進來的V類型。

Future

Executor是Runnable和Callable的調度容器,而Future就是對于具體的Runnable或者Callable任務的執行結果進行取消、查詢是否完成、獲取結果、設置結果操作。get方法會阻塞,直到任務返回結果(Future簡介)。Future聲明如下:

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,而RunnableFuture實現了Runnbale又實現了Futrue這兩個接口,

public class FutureTask<V> implements RunnableFuture<V> { } 復制代碼

RunnableFuture

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, 由構造函數注入依賴。

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()函數轉換為Callable類型,即FutureTask最終都是執行Callable類型的任務。該適配函數的實現如下 :

public static <T> Callable<T> callable(Runnable task, T result) { if (task == null) throw new NullPointerException(); return new RunnableAdapter<T>(task, result); } 復制代碼

RunnableAdapter適配器

/** * 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實現了Runnable,因此它既可以通過Thread包裝來直接執行,也可以提交給ExecuteService來執行。

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

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

簡單示例

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.println("runnable demo : " + fibc(20)); } }).start(); } /** * 其中Runnable實現的是void run()方法,無返回值;Callable實現的是 V * call()方法,并且可以返回執行結果。其中Runnable可以提交給Thread來包裝下 * ,直接啟動一個線程來執行,而Callable則一般都是提交給ExecuteService來執行。 */ static void futureDemo() { try { /** * 提交runnable則沒有返回值, future沒有數據 */ Future<?> result = mExecutor.submit(new Runnable() { @Override public void run() { fibc(20); } }); System.out.println("future result from runnable : " + result.get()); /** * 提交Callable, 有返回值, future中能夠獲取返回值 */ Future<Integer> result2 = mExecutor.submit(new Callable<Integer>() { @Override public Integer call() throws Exception { return fibc(20); } }); System.out .println("future result from callable : " + result2.get()); /** * FutureTask則是一個RunnableFuture<V>,即實現了Runnbale又實現了Futrue<V>這兩個接口, * 另外它還可以包裝Runnable(實際上會轉換為Callable)和Callable * <V>,所以一般來講是一個符合體了,它可以通過Thread包裝來直接執行,也可以提交給ExecuteService來執行 * ,并且還可以通過v get()返回執行結果,在線程體沒有執行完成的時候,主線程一直阻塞等待,執行完則直接返回結果。 */ FutureTask<Integer> futureTask = new FutureTask<Integer>( new Callable<Integer>() { @Override public Integer call() throws Exception { return fibc(20); } }); // 提交futureTask mExecutor.submit(futureTask) ; System.out.println("future result from futureTask : " + futureTask.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } /** * 效率底下的斐波那契數列, 耗時的操作 * * @param num * @return */ static int fibc(int num) { if (num == 0) { return 0; } if (num == 1) { return 1; } return fibc(num - 1) + fibc(num - 2); } } 復制代碼

來源:blog.csdn.net/bboyfeiyu/a…

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Java中的Runnable、Callable、Future、FutureTask的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国语对白做受69 | 中文字幕成人一区 | av黄色在线播放 | 午夜激情婷婷 | caoprom超碰 | 午夜福利电影一区二区 | 30一40一50女人毛片 | 一级黄色片网址 | 日本免费网站在线观看 | 4444亚洲人成无码网在线观看 | 久久久久99精品成人片 | 国产精品成人免费看片 | 亚洲一区视频网站 | 欧美有码在线观看 | 日日操夜夜干 | 深夜福利1000 | 欧美日韩综合精品 | 色一情一乱一伦一区二区三区 | 加勒比精品 | 丰满肉嫩西川结衣av | 黄色喷水网站 | 秋霞欧美视频 | 亚洲天堂av网 | 久久影视精品 | 久久久久国产精品午夜一区 | 超碰国产在线 | 国产午夜在线视频 | 国产人妖ts重口系列网站观看 | 欧美激情亚洲 | 日韩影院在线 | 在线观看免费黄网站 | caoprom在线视频| 国产一区二区在线观看视频 | 手机av网站| jizz日韩| 亚洲精品一区二区三区不卡 | 情侣自拍av | 欧美 日韩 国产 成人 在线观看 | 国产精品视频你懂的 | 国产视频你懂得 | 亚洲精品人妻av | 免费国产黄 | 人妻一区二区三区免费 | 欧美做爰猛烈床戏大尺度 | 欧美老熟妇一区二区 | 亚洲天堂免费av | 欧美一级淫片 | 国产精品一区二区三区在线播放 | 欧美精品一区二 | 久久福利一区 | www.国产91| 另类小说色综合 | 成人影视免费 | 美女又爽又黄视频 | 丝袜调教91porn| 在线a毛片 | 国内9l自拍 | 在厨房拨开内裤进入毛片 | 五月婷婷在线视频 | 色呦呦在线播放 | 国产精品免 | 四虎网址大全 | 免费观看黄色小视频 | 黑人黄色录像 | 99精品在线免费视频 | 国产在线毛片 | 午夜色网站| 国产日韩视频在线观看 | 免费的三级网站 | 中国老头同性xxxxx | 欧美精品一二三四区 | va免费视频 | 午夜片在线观看 | 欧美性成人 | 欧洲成人在线观看 | 日韩精品一区二区视频 | 国产精品美女一区二区 | 久久尤物| 欧美资源网 | gogo人体做爰大胆视频 | 精品国产午夜福利在线观看 | 国产精品福利一区二区 | 日本少妇激三级做爰在线 | 日本a在线天堂 | 国产黄色自拍视频 | 免费看美女被靠到爽的视频 | 能免费看18视频网站 | 男女日日 | 97超碰在| 向日葵视频在线 | 99性趣网| 日韩久久久久久久久久 | 大色av | 在线能看的av网站 | 久久成人a | 日韩视频精品在线 | 精品一区二区三区人妻 | 琪琪秋霞午夜被窝电影网 | 日韩啪 |