Java利用future及時獲取多線程運行結果
Future接口是Java標準API的一部分,在java.util.concurrent包中。Future接口是Java線程Future模式的實現(xiàn),可以來進行異步計算。
有了Future就可以進行三段式的編程了,1.啟動多線程任務2.處理其他事3.收集多線程任務結果。從而實現(xiàn)了非阻塞的任務調(diào)用。在途中遇到一個問題,那就是雖然能異步獲取結果,但是Future的結果需要通過isdone來判斷是否有結果,或者使用get()函數(shù)來阻塞式獲取執(zhí)行結果。這樣就不能實時跟蹤其他線程的結果狀態(tài)了,所以直接使用get還是要慎用,最好配合isdone來使用。
這里有一種更好的方式來實現(xiàn)對任意一個線程運行完成后的結果都能及時獲取的辦法:使用CompletionService,它內(nèi)部添加了阻塞隊列,從而獲取future中的值,然后根據(jù)返回值做對應的處理。一般future使用和CompletionService使用的兩個測試案例如下:
import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** * 多線程執(zhí)行,異步獲取結果 * * @author i-clarechen * */ public class AsyncThread { public static void main(String[] args) { AsyncThread t = new AsyncThread(); List<Future<String>> futureList = new ArrayList<Future<String>>(); t.generate(3, futureList); t.doOtherThings(); t.getResult(futureList); } /** * 生成指定數(shù)量的線程,都放入future數(shù)組 * * @param threadNum * @param fList */ public void generate(int threadNum, List<Future<String>> fList) { ExecutorService service = Executors.newFixedThreadPool(threadNum); for (int i = 0; i < threadNum; i++) { Future<String> f = service.submit(getJob(i)); fList.add(f); } service.shutdown(); } /** * other things */ public void doOtherThings() { try { for (int i = 0; i < 3; i++) { System.out.println("do thing no:" + i); Thread.sleep(1000 * (new Random().nextInt(10))); } } catch (InterruptedException e) { e.printStackTrace(); } } /** * 從future中獲取線程結果,打印結果 * * @param fList */ public void getResult(List<Future<String>> fList) { ExecutorService service = Executors.newSingleThreadExecutor(); service.execute(getCollectJob(fList)); service.shutdown(); } /** * 生成指定序號的線程對象 * * @param i * @return */ public Callable<String> getJob(final int i) { final int time = new Random().nextInt(10); return new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(1000 * time); return "thread-" + i; } }; } /** * 生成結果收集線程對象 * * @param fList * @return */ public Runnable getCollectJob(final List<Future<String>> fList) { return new Runnable() { public void run() { for (Future<String> future : fList) { try { while (true) { if (future.isDone() && !future.isCancelled()) { System.out.println("Future:" + future + ",Result:" + future.get()); break; } else { Thread.sleep(1000); } } } catch (Exception e) { e.printStackTrace(); } } } }; } }
運行結果打印和future放入列表時的順序一致,為0,1,2:
do thing no:0 do thing no:1 do thing no:2 Future:java.util.concurrent.FutureTask@68e1ca74,Result:thread-0 Future:java.util.concurrent.FutureTask@3fb2bb77,Result:thread-1 Future:java.util.concurrent.FutureTask@6f31a24c,Result:thread-2
下面是先執(zhí)行完的線程先處理的方案:
import java.util.Random; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingDeque; public class testCallable { public static void main(String[] args) { try { completionServiceCount(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } /** * 使用completionService收集callable結果 * @throws ExecutionException * @throws InterruptedException */ public static void completionServiceCount() throws InterruptedException, ExecutionException { ExecutorService executorService = Executors.newCachedThreadPool(); CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>( executorService); int threadNum = 5; for (int i = 0; i < threadNum; i++) { completionService.submit(getTask(i)); } int sum = 0; int temp = 0; for(int i=0;i<threadNum;i++){ temp = completionService.take().get(); sum += temp; System.out.print(temp + "\t"); } System.out.println("CompletionService all is : " + sum); executorService.shutdown(); } public static Callable<Integer> getTask(final int no) { final Random rand = new Random(); Callable<Integer> task = new Callable<Integer>() { @Override public Integer call() throws Exception { int time = rand.nextInt(100)*100; System.out.println("thead:"+no+" time is:"+time); Thread.sleep(time); return no; } }; return task; } }
運行結果為最先結束的線程結果先被處理:
thead:0 time is:4200 thead:1 time is:6900 thead:2 time is:2900 thead:3 time is:9000 thead:4 time is:7100 0 1 4 3 CompletionService all is : 10
總結
以上就是本文關于Java利用future及時獲取線程運行結果的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以參閱:Java多線程ForkJoinPool實例詳解、Java通過賣票理解多線程、Java多線程之readwritelock讀寫分離的實現(xiàn)代碼等,有什么問題可以隨時留言,歡迎大家交流討論。
相關文章
SpringBoot集成ElaticJob定時器的實現(xiàn)代碼
這篇文章主要介紹了SpringBoot集成ElaticJob定時器的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06mybatis Example的Criteria用法:or與isNull詳解
這篇文章主要介紹了mybatis Example的Criteria用法:or與isNull詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12解決java maven項目找不到jconsole-1.8.0.jar和tools-1.8.0.jar包問題
這篇文章主要介紹了解決java maven項目找不到jconsole-1.8.0.jar和tools-1.8.0.jar包問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08SpringBoot如何統(tǒng)一清理數(shù)據(jù)
這篇文章主要介紹了SpringBoot如何統(tǒng)一清理數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Java easyexcel導出報內(nèi)存溢出的問題解決
在Java開發(fā)時,使用EasyExcel處理大數(shù)據(jù)量導出可能遇到內(nèi)存溢出問題,本文深入分析了內(nèi)存溢出的原因,并提出了優(yōu)化策略,感興趣的可以了解一下2024-10-10JAVA動態(tài)代理模式(從現(xiàn)實生活角度理解代碼原理)
本文主要介紹了JAVA動態(tài)代理模式(從現(xiàn)實生活角度理解代碼原理)的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03