在Java編程中揍瑟,Java利用future及時(shí)獲取線程運(yùn)行結(jié)果的方法有兩種,第一種利用Future的isdone()和get()結(jié)合獲取乍炉,第二種利用java的concurrent包中的CompletionService提供的方法非阻塞獲取月培。
Future接口是Java標(biāo)準(zhǔn)API的一部分嘁字,在java.util.concurrent包中。Future接口是Java線程Future模式的實(shí)現(xiàn)杉畜,可以來(lái)進(jìn)行異步計(jì)算。
有了Future就可以進(jìn)行三段式的編程了衷恭,1.啟動(dòng)多線程任務(wù)2.處理其他事3.收集多線程任務(wù)結(jié)果此叠。從而實(shí)現(xiàn)了非阻塞的任務(wù)調(diào)用。在途中遇到一個(gè)問(wèn)題随珠,那就是雖然能異步獲取結(jié)果灭袁,但是Future的結(jié)果需要通過(guò)isdone來(lái)判斷是否有結(jié)果,或者使用get()函數(shù)來(lái)阻塞式獲取執(zhí)行結(jié)果窗看。這樣就不能實(shí)時(shí)跟蹤其他線程的結(jié)果狀態(tài)了茸歧,所以直接使用get還是要慎用,最好配合isdone來(lái)使用显沈。
有一種更好的方式來(lái)實(shí)現(xiàn)對(duì)任意一個(gè)線程運(yùn)行完成后的結(jié)果都能及時(shí)獲取的辦法:使用CompletionService软瞎,它內(nèi)部添加了阻塞隊(duì)列,從而獲取future中的值拉讯,然后根據(jù)返回值做對(duì)應(yīng)的處理涤浇。
利用Future的get()方法阻塞式獲取
/**
* 多線程執(zhí)行,異步獲取結(jié)果
*/
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ù)組
*/
public void generate(int threadNum, List<Future<String>> fList) {
ExecutorService service = Executors.newFixedThreadPool(threadNum);
for (int i = 0; i < threadNum; i++) {
//submit與execute的區(qū)別在于submit有返回值只锭,一般對(duì)應(yīng)于callable
Future<String> f = service.submit(getJob(i));
fList.add(f);
}
service.shutdown();
}
/**
* * other things
*
*/
public void doOtherThings() {
System.out.println("now is yes");
}
/**
* * 從future中獲取線程結(jié)果,打印結(jié)果
*
*/
public void getResult(List<Future<String>> fList) {
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(getCollectJob(fList));
service.shutdown();
}
/**
* * 生成指定序號(hào)的線程對(duì)象
*
*/
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ì)象
*/
public Runnable getCollectJob(final List<Future<String>> fList) {
return new Runnable() {
public void run() {
for (Future<String> future : fList) {
try {
//如果不采用while(true)將會(huì)是阻塞式獲取結(jié)果
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();
}
}
}
};
}
}
使用CompletionService非阻塞獲取多線程返回值
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;
}
可以看出院尔,CompletionService編程更友好蜻展,簡(jiǎn)潔易懂。