复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28static 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"); }
复制代码
1
2
3
4// 输出 get timeout delay 5000 wait shutdown
最后
以上就是现实学姐最近收集整理的关于Future.get抛出TimeoutException,并不会中断或者取消任务的全部内容,更多相关Future内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复