Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4608072
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: Java

2018-09-13 12:56:44


As stated in the documentation, you cannot reuse an ExecutorService that has been shut down. I'd recommend against any workarounds, since (a) they may not work as expected in all situations; and (b) you can achieve what you want using standard classes.

You must either

  1. instantiate a new ExecutorService; or

  2. not terminate the ExecutorService.

The first solution is easily implemented, so I won't detail it.

For the second, since you want to execute an action once all the submitted tasks have finished, you might take a look at ExecutorCompletionService and use it instead. It wraps an ExecutorService which will do the thread management, but the runnables will get wrapped into something that will tell the ExecutorCompletionService when they have finished, so it can report back to you:

ExecutorService executor = ...; ExecutorCompletionService ecs = new ExecutorCompletionService(executor); for (int i = 0; i < totalTasks; i++) { ... ecs.submit(...); ... } for (int i = 0; i < totalTasks; i++) { ecs.take(); }

The method take() on the ExecutorCompletionService class will block until a task has finished (either normally or abruptly). It will return a Future, so you can check the results if you wish.

I hope this can help you, since I didn't completely understand your problem.

阅读(644) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~