在之前的工作中,主要是寫業(yè)務(wù)代碼,線程使用得很少,也沒有考慮執(zhí)行線程后如何獲取返回值的問題,而線程中的run()方法是沒有返回值的。那如果在線程執(zhí)行完畢后想要獲取對(duì)應(yīng)的值要怎么做?借著最近的學(xué)習(xí)總結(jié)一下。線程返回值主要有幾種方式。
- 通過代碼做循環(huán)判斷
- 使用join
- 使用Callable接口和FutureTask
- 使用線程池
下面通過demo做一下演示
public class MyThreadReturn implements Runnable {
/** 模擬線程執(zhí)行完畢后主程序要獲取的值*/
private String returnValue;
@Override
public void run() {
System.out.println("線程執(zhí)行......");
/** 模擬IO*/
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程執(zhí)行完畢......");
returnValue = "hello world!!!";
}
public String getReturnValue(){
return returnValue;
}
public static void main(String[] args) {
MyThreadReturn myThreadReturn = new MyThreadReturn();
Thread thread = new Thread(myThreadReturn);
thread.start();
System.out.println(myThreadReturn.getReturnValue());
}
}
以上代碼因?yàn)镸yThreadReturn線程需要5秒執(zhí)行完畢,主線程中并不能順利獲取到returnValue
null
線程執(zhí)行.....
將代碼做如下修改
通過循環(huán)判斷
該方法本質(zhì)是自己控制業(yè)務(wù)邏輯
public static void main(String[] args) throws InterruptedException {
MyThreadReturn myThreadReturn = new MyThreadReturn();
Thread thread = new Thread(myThreadReturn);
thread.start();
/** 通過while循環(huán)判斷*/
while (myThreadReturn.getReturnValue() == null){
Thread.sleep(1000);
}
System.out.println(myThreadReturn.getReturnValue());
}
使用join
使用join方法可以讓子線程執(zhí)行完畢后再執(zhí)行主線程,本質(zhì)和通過循環(huán)判斷一樣
public static void main(String[] args) throws InterruptedException {
MyThreadReturn myThreadReturn = new MyThreadReturn();
Thread thread = new Thread(myThreadReturn);
thread.start();
/** 使用join*/
thread.join();
System.out.println(myThreadReturn.getReturnValue());
}
使用Callable接口和FutureTask
代碼如下,通過FutureTask的get()方法獲取返回值
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("線程執(zhí)行......");
Thread.sleep(5000);
System.out.println("線程執(zhí)行完畢......");
return "hello world!!!";
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> futureTask = new FutureTask<>(new MyCallable());
/***
* futureTask 實(shí)現(xiàn)了 Runnable接口
* 所以新建線程的時(shí)候可以傳入futureTask
* FutureTask重寫的run方法中實(shí)際是調(diào)用了Callable接口在call()方法
* 所以執(zhí)行線程的時(shí)候回執(zhí)行call方法的內(nèi)容
*/
Thread thread = new Thread(futureTask);
thread.start();
String value = futureTask.get();
System.out.println(value);
}
}
使用線程池
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
Future<String> submit = executorService.submit(new MyCallable());
System.out.println(submit.get());
}
以上代碼返回值均為
線程執(zhí)行......
線程執(zhí)行完畢......
hello world!!!