[英]calling ExecutorService.shutDown() in java
i am starting to learn the ExecutorService class. 我开始学习ExecutorService类。 The documentation (and tutorials online) say to always call ExecutorService.shutDown() to reclaim resources.
文档(和在线教程)说总是调用ExecutorService.shutDown()来回收资源。 however, the documentation also says that after you call shutDown(), no new tasks will be accepted.
但是,文档还说在调用shutDown()之后,不会接受任何新任务。 so, my question is, do i have always have to instantiate a new ExecutorService whenever i need to parallelize data processing?
所以,我的问题是,每当我需要并行化数据处理时,我是否始终必须实例化一个新的ExecutorService?
right now i have a List of Callable objects, and i do the following. 现在我有一个可调用对象列表,我执行以下操作。
public void someMethod() {
List<OuterCallable> outerCallables = getOuterCallables();
ExecutorService executor = Executor.newFixedThreadPool(NUM_CPUS);
executor.invokeAll(tasks);
executor.shutDown();
}
however, my OuterCallable also splits data or executes data processing in parallel using InnerCallable. 但是,我的OuterCallable还使用InnerCallable并行分割数据或执行数据处理。
public class OuterCallable implements Callable<Long> {
public Long call() throws Exception {
long result = 0L;
List<InnerCallable> innerCallables = getInnerCallables();
ExecutorServices executor = Executor.newFixedThreadPool(NUM_CPUS);
executor.invokeAll(tasks);
executor.shutDown();
return result;
}
}
i can't remember if it was for ExecutorService or the Fork/Join approach, but i remember the documentations and tutorials saying that the actual parallel procedure to manipulate data should not involve I/O operations and everything should be done in memory. 我不记得它是用于ExecutorService还是Fork / Join方法,但是我记得文档和教程说操作数据的实际并行过程不应该涉及I / O操作,一切都应该在内存中完成。 however, in my InnerCallable, i am actually making JDBC calls (not shown here).
但是,在我的InnerCallable中,我实际上正在进行JDBC调用(此处未显示)。
ultimately, the way i am using ExecutorService works, but i still have lingering concerns. 最终,我使用ExecutorService的方式有效,但我仍然有一些挥之不去的担忧。
as a last concern, i was trying to research a little bit on Fork/Join vs ExecutorService. 作为最后一个问题,我试图研究一下Fork / Join vs ExecutorService。 i came across an article that completely blasted the Fork/Join API/classes.
我遇到了一篇完全抨击Fork / Join API /类的文章。 is it worth it to learn Fork/Join?
学习Fork / Join值得吗? i saw a few articles on stackoverflow and elsewhere, where tests are used to compare Fork/Join vs ExecutorService, and there are graphs showing better CPU usage of Fork/Join vs ExecutorService (via Windows Task Manager).
我在stackoverflow和其他地方看到了一些文章,其中测试用于比较Fork / Join和ExecutorService,并且有图表显示了Fork / Join vs ExecutorService的更好的CPU使用率(通过Windows任务管理器)。 however, when i use ExecutorService (JDK 1.7.x), my CPU usage is max.
但是,当我使用ExecutorService(JDK 1.7.x)时,我的CPU使用率是最大值。 has ExecutorService improved with the latest JDK?
使用最新的JDK改进了ExecutorService吗?
any help/guidance is appreciated. 任何帮助/指导表示赞赏。
You should add awaitTermination
calls, because shutDown
returns without waiting for Callables to finish. 您应该添加
awaitTermination
调用,因为shutDown
返回而不等待Callables完成。 Other than that, 除此之外,
OuterCallable
s have sequential dependencies? OuterCallable
有顺序依赖? If so, your approach is fine, but using ForkJoinPool would be preferable because it will keep the number of worker threads low. Callable
s to a single Executor
. Callable
s集合提交给一个Executor
。 someMethod
, might as well instantiate it there as you are doing. someMethod
,那么也可以像你一样在那里实例化它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.