java多線程返回值使用示例(callable與futuretask)
Callable接口類似于Runnable,從名字就可以看出來了,但是Runnable不會(huì)返回結(jié)果,并且無法拋出返回結(jié)果的異常,而Callable功能更強(qiáng)大一些,被線程執(zhí)行后,可以返回值,這個(gè)返回值可以被Future拿到,也就是說,F(xiàn)uture可以拿到異步執(zhí)行任務(wù)的返回值,下面來看一個(gè)簡單的例子
package com.future.test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class MyTest {
// 接收在run方法中捕獲的異常,然后自定義方法拋出異常
//private static Throwable exception;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String result = "";
ExecutorService executor = Executors.newSingleThreadExecutor();
FutureTask<String> future =
new FutureTask<String>(new Callable<String>() {//使用Callable接口作為構(gòu)造參數(shù)
public String call() {
//真正的任務(wù)在這里執(zhí)行,這里的返回值類型為String,可以為任意類型
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
//exception = e;
//e.printStackTrace();
}
return "11111";
}});
executor.execute(future);
//在這里可以做別的任何事情
try {
result = future.get(5000, TimeUnit.MILLISECONDS); //取得結(jié)果,同時(shí)設(shè)置超時(shí)執(zhí)行時(shí)間為5秒。同樣可以用future.get(),不設(shè)置執(zhí)行超時(shí)時(shí)間取得結(jié)果
} catch (InterruptedException e) {
//System.out.println("任務(wù)已經(jīng)取消");
future.cancel(true);
} catch (ExecutionException e) {
future.cancel(true);
} catch (TimeoutException e) {
future.cancel(true);
} finally {
executor.shutdown();
}
System.out.println("result:"+result);
}
/* public void throwException() throws FileNotFoundException, IOException {
if (exception instanceof FileNotFoundException)
throw (FileNotFoundException) exception;
if (exception instanceof IOException)
throw (IOException) exception;
}*/
}
相關(guān)文章
springboot使用Thymeleaf報(bào)錯(cuò)常見的幾種解決方案
這篇文章主要介紹了springboot使用Thymeleaf報(bào)錯(cuò)常見的幾種解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Java中的三元運(yùn)算(三目運(yùn)算)以后用得到!
Java提供了一個(gè)三元運(yùn)算符,可以同時(shí)操作3個(gè)表達(dá)式,下面這篇文章主要給大家介紹了關(guān)于Java中三元運(yùn)算(三目運(yùn)算)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10Java中static修飾的靜態(tài)變量、方法及代碼塊的特性與使用
這篇文章主要介紹了Java中static修飾的靜態(tài)變量、方法及代碼塊的特性與使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04深入學(xué)習(xí)SpringCloud之SpringCloud簡介
Spring Cloud是一個(gè)一站式的開發(fā)分布式系統(tǒng)的框架,為開發(fā)者提供了一系列的構(gòu)建分布式系統(tǒng)的工具集,本文給大家介紹springcloud的相關(guān)知識,感興趣的朋友跟隨一起看看吧2021-04-04spring mvc使用@InitBinder標(biāo)簽對表單數(shù)據(jù)綁定的方法
這篇文章主要介紹了spring mvc使用@InitBinder標(biāo)簽對表單數(shù)據(jù)綁定的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03如何使用Idea中的 Deployment 實(shí)現(xiàn)打包自動(dòng)部署
這篇文章主要介紹了使用Idea中的 Deployment 實(shí)現(xiàn)打包自動(dòng)部署,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08SpringBoot項(xiàng)目使用mybatis-plus逆向自動(dòng)生成全套代碼
在JavaWeb工程中,每一個(gè)SSM新項(xiàng)目或者說是SpringBoot項(xiàng)目也好,都少不了model、controller、service、dao等層次的構(gòu)建。使用mybatis-plus逆向可以自動(dòng)生成,感興趣的可以了解一下2021-09-09