我是靠谱客的博主 现实学姐,这篇文章主要介绍Future.get抛出TimeoutException,并不会中断或者取消任务,现在分享给大家,希望可以做个参考。

static ExecutorService executorService = Executors.newCachedThreadPool();
/**
* get 出现TimeoutException,并不会中断或者取消运算线程
* @throws InterruptedException
*/
@Test
void testGetTimeoutException() throws InterruptedException {
final Future<Long> future = executorService.submit(() -> {
delay(3000);
System.out.println("delay 3000");
return 888L;
});
try {
final long value = future.get(1, TimeUnit.SECONDS);
System.out.println("value = " + value);
Assertions.fail();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
// get timeout
System.out.println("get timeout");
}
executorService.shutdown();
executorService.awaitTermination(100, TimeUnit.SECONDS);
System.out.println("wait shutdown");
}
// 输出
get timeout
delay 5000
wait shutdown

 

最后

以上就是现实学姐最近收集整理的关于Future.get抛出TimeoutException,并不会中断或者取消任务的全部内容,更多相关Future内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部