java 多线程Callable和Runable执行顺序问题详解
詳見: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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java必备:java入门、java学习
- 下一篇: netapp更换硬盘