RedSpider1 / concurrent

这是RedSpider社区成员原创与维护的Java多线程系列文章。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

2.2.3FutureTask类例程,没有抛出异常

mapleafly opened this issue · comments

// 自定义Callable,与上面一样
class Task implements Callable{
@OverRide
public Integer call() throws Exception {
// 模拟计算需要一秒
Thread.sleep(1000);
return 2;
}
public static void main(String args[]){
// 使用
ExecutorService executor = Executors.newCachedThreadPool();
FutureTask futureTask = new FutureTask<>(new Task());
executor.submit(futureTask);
System.out.println(futureTask.get());
}
}

futureTask.get()需要抛出异常

严谨, 感谢反馈, 已修复