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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 多线程Callable和Runable执行顺序问题详解

發(fā)布時間:2025/5/22 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 多线程Callable和Runable执行顺序问题详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

詳見:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt125

毫無疑問 Runnable會進(jìn)行異步執(zhí)行,此處不多說,主要說明Callable的使用,看實例:

1、

1 2 3 4 5 6 7 8 9 10 11 public?class?ThreadTest?{ ????public?static?void?main(String[]?args)?throws?InterruptedException,?ExecutionException?{ ????????ExecutorService?executor?=?Executors.newFixedThreadPool(4); ????????MyTread?m1?=?new?MyTread(); ????????Future?f?=?executor.submit(m1); ????????//?System.out.println(f.get()); ????????executor.shutdown(); ????????System.out.println("主線程執(zhí)行完了"); ????} ?? }
1 2 3 4 5 6 7 8 9 10 11 12 13 ?? class?MyTread?implements?Callable<String>?{ ????@Override ????public?String?call()?{ ????????try?{ ????????????System.out.println("線程調(diào)度:"?+?Thread.currentThread()); ????????????TimeUnit.SECONDS.sleep(3); ????????}?catch?(InterruptedException?e)?{ ????????????e.printStackTrace(); ????????} ????????return?"123"; ????} }

此程序雖然獲取了call方法的返回值,但是沒有做處理,所以主線程main和m1同時執(zhí)行,執(zhí)行結(jié)果如下:

主線程執(zhí)行完了

線程調(diào)度:Thread[pool-1-thread-1,5,main]

?

2、

1 2 3 4 5 6 7 8 9 10 11 public?class?ThreadTest?{ ????public?static?void?main(String[]?args)?throws?InterruptedException,?ExecutionException?{ ????????ExecutorService?executor?=?Executors.newFixedThreadPool(4); ????????MyTread?m1?=?new?MyTread(); ????????Future?f?=?executor.submit(m1); ????????System.out.println(f.get());?//?進(jìn)行了輸出 ????????executor.shutdown(); ????????System.out.println("主線程執(zhí)行完了"); ????} ?? }

?

1 2 3 4 5 6 7 8 9 10 11 12 class?MyTread?implements?Callable<String>?{ ????@Override ????public?String?call()?{ ????????try?{ ????????????System.out.println("線程調(diào)度:"?+?Thread.currentThread()); ????????????TimeUnit.SECONDS.sleep(3); ????????}?catch?(InterruptedException?e)?{ ????????????e.printStackTrace(); ????????} ????????return?"123"; ????} }

?

在2中,對m1中call方法的返回值在main中進(jìn)行了處理(輸出),所以在此種情況下,main需要等待m1執(zhí)行完,再繼續(xù)執(zhí)行,執(zhí)行結(jié)果如下

線程調(diào)度:Thread[pool-1-thread-1,5,main]

123

主線程執(zhí)行完了

?

3、再看當(dāng)主線程中同時啟動兩個由Callable生成的線程時

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public?class?ThreadTest?{ ????public?static?void?main(String[]?args)?throws?InterruptedException,?ExecutionException?{ ????????ExecutorService?executor?=?Executors.newFixedThreadPool(4); ????????MyTread?m1?=?new?MyTread(); ????????MyTread2?m2?=?new?MyTread2(); ????????Long?time1?=?TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); ????????Future?f?=?executor.submit(m1); ????????Future?f2?=?executor.submit(m2); ????????System.out.println(f.get());?//?進(jìn)行了輸出m1 ????????System.out.println(f2.get());?//?進(jìn)行了輸出m2 ????????executor.shutdown(); ????????System.out.println("主線程執(zhí)行完了"); ????????Long?time2?=?TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); ????????System.out.println("主線程等待了"?+?(time2?-?time1)?+?"秒"); ????} ?? }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class?MyTread?implements?Callable<String>?{ ????@Override ????public?String?call()?{ ????????try?{ ????????????System.out.println("線程調(diào)度:"?+?Thread.currentThread()); ????????????TimeUnit.SECONDS.sleep(3); ????????}?catch?(InterruptedException?e)?{ ????????????e.printStackTrace(); ????????} ????????return?"123"; ????} } ?? class?MyTread2?implements?Callable<String>?{ ????@Override ????public?String?call()?{ ????????try?{ ????????????System.out.println("線程調(diào)度2:"?+?Thread.currentThread()); ????????????TimeUnit.SECONDS.sleep(3); ????????}?catch?(InterruptedException?e)?{ ????????????e.printStackTrace(); ????????} ????????return?"abc"; ????} }

當(dāng)不對m1和m2做輸出時,main和m1、m2并發(fā)執(zhí)行,當(dāng)對m1和m2中任意一個的返回值進(jìn)行處理的時候,main需要等待,但是m1和m2之前仍然是并發(fā)執(zhí)行,執(zhí)行結(jié)果如下:

線程調(diào)度2:Thread[pool-1-thread-2,5,main]

線程調(diào)度:Thread[pool-1-thread-1,5,main]

123

abc

主線程執(zhí)行完了

主線程等待了3秒

總結(jié)

以上是生活随笔為你收集整理的java 多线程Callable和Runable执行顺序问题详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。