Java利用future及時(shí)獲取多線程運(yùn)行結(jié)果
Future接口是Java標(biāo)準(zhǔn)API的一部分,在java.util.concurrent包中。Future接口是Java線程Future模式的實(shí)現(xiàn),可以來進(jìn)行異步計(jì)算。
有了Future就可以進(jìn)行三段式的編程了,1.啟動(dòng)多線程任務(wù)2.處理其他事3.收集多線程任務(wù)結(jié)果。從而實(shí)現(xiàn)了非阻塞的任務(wù)調(diào)用。在途中遇到一個(gè)問題,那就是雖然能異步獲取結(jié)果,但是Future的結(jié)果需要通過isdone來判斷是否有結(jié)果,或者使用get()函數(shù)來阻塞式獲取執(zhí)行結(jié)果。這樣就不能實(shí)時(shí)跟蹤其他線程的結(jié)果狀態(tài)了,所以直接使用get還是要慎用,最好配合isdone來使用。
這里有一種更好的方式來實(shí)現(xiàn)對(duì)任意一個(gè)線程運(yùn)行完成后的結(jié)果都能及時(shí)獲取的辦法:使用CompletionService,它內(nèi)部添加了阻塞隊(duì)列,從而獲取future中的值,然后根據(jù)返回值做對(duì)應(yīng)的處理。一般future使用和CompletionService使用的兩個(gè)測(cè)試案例如下:
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í)行,異步獲取結(jié)果
*
* @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中獲取線程結(jié)果,打印結(jié)果
*
* @param fList
*/
public void getResult(List<Future<String>> fList) {
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(getCollectJob(fList));
service.shutdown();
}
/**
* 生成指定序號(hào)的線程對(duì)象
*
* @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;
}
};
}
/**
* 生成結(jié)果收集線程對(duì)象
*
* @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();
}
}
}
};
}
}
運(yùn)行結(jié)果打印和future放入列表時(shí)的順序一致,為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結(jié)果
* @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;
}
}
運(yùn)行結(jié)果為最先結(jié)束的線程結(jié)果先被處理:
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
總結(jié)
以上就是本文關(guān)于Java利用future及時(shí)獲取線程運(yùn)行結(jié)果的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以參閱:Java多線程ForkJoinPool實(shí)例詳解、Java通過賣票理解多線程、Java多線程之readwritelock讀寫分離的實(shí)現(xiàn)代碼等,有什么問題可以隨時(shí)留言,歡迎大家交流討論。
相關(guān)文章
SpringBoot集成ElaticJob定時(shí)器的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringBoot集成ElaticJob定時(shí)器的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
mybatis Example的Criteria用法:or與isNull詳解
這篇文章主要介紹了mybatis Example的Criteria用法:or與isNull詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
解決java maven項(xiàng)目找不到j(luò)console-1.8.0.jar和tools-1.8.0.jar包問題
這篇文章主要介紹了解決java maven項(xiàng)目找不到j(luò)console-1.8.0.jar和tools-1.8.0.jar包問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
SpringBoot如何統(tǒng)一清理數(shù)據(jù)
這篇文章主要介紹了SpringBoot如何統(tǒng)一清理數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Java easyexcel導(dǎo)出報(bào)內(nèi)存溢出的問題解決
在Java開發(fā)時(shí),使用EasyExcel處理大數(shù)據(jù)量導(dǎo)出可能遇到內(nèi)存溢出問題,本文深入分析了內(nèi)存溢出的原因,并提出了優(yōu)化策略,感興趣的可以了解一下2024-10-10
JAVA動(dòng)態(tài)代理模式(從現(xiàn)實(shí)生活角度理解代碼原理)
本文主要介紹了JAVA動(dòng)態(tài)代理模式(從現(xiàn)實(shí)生活角度理解代碼原理)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
Java的Spring框架中AOP項(xiàng)目的一般配置和部署教程
這篇文章主要介紹了Java的Spring框架中AOP項(xiàng)目的一般配置和部署教程,AOP面向方面編程的項(xiàng)目部署結(jié)構(gòu)都比較類似,因而也被看作是Spring的一種設(shè)計(jì)模式使用,需要的朋友可以參考下2016-04-04

