import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CalBFutrueTest {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
final Future<String> submit = newCachedThreadPool.submit(new TaskCallback());
System.out.println(Thread.currentThread().getName()+"-主线程开始执行");
new Thread(new Runnable() {
public void run() {
try {
String result = submit.get();
System.out.println(result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}).start();
System.out.println("主线程执行任务...");
if(newCachedThreadPool!=null) {
//关闭线程池
newCachedThreadPool.shutdown();
}
}
}
class TaskCallback implements Callable<String> {
public String call() throws Exception {
System.out.println("子线程正在执行任务,请等待5s");
Thread.sleep(5000);
System.out.println("子线程 任务结束");
return "abab";
}
}
执行结果
子线程正在执行任务,请等待5s
main-主线程开始执行
主线程执行任务...
子线程 任务结束
abab