简体   繁体   English

在java中调用ExecutorService.shutDown()

[英]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的方式有效,但我仍然有一些挥之不去的担忧。

  1. is my approach above good programming practice using ExecutorService? 我的方法是使用ExecutorService进行良好的编程实践吗?
  2. should i be using a singleton instance of ExecutorService? 我应该使用ExecutorService的单例实例吗?
  3. should i be not only avoiding I/O operations inside my parallel methods but also JDBC calls as well? 我不仅应该避免并行方法中的I / O操作,还要避免JDBC调用吗?

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, 除此之外,

  1. Do you 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. 如果是这样,你的方法很好,但使用ForkJoinPool会更好,因为它会保持较低的工作线程数。 If not, it would be better to submit a big flattened collection of Callable s to a single Executor . 如果没有,最好将一个大的扁平化Callable s集合提交给一个Executor
  2. Only if you want to use the ExecutorService in several different routines and want to avoid passing it around. 仅当您想在几个不同的例程中使用ExecutorService并且希望避免传递它时。 If it's only used in someMethod , might as well instantiate it there as you are doing. 如果它只在someMethod ,那么也可以像你一样在那里实例化它。
  3. You should avoid I/O routines that will take a long time to complete. 您应该避免需要很长时间才能完成的I / O例程。 If your ExecutorService has 4 worker threads and all of them are blocking on I/O, the JVM won't be using the CPU at all, even though other Callables may be waiting to do CPU-intensive work. 如果您的ExecutorService有4个工作线程且所有这些线程都在I / O上阻塞,那么JVM根本不会使用CPU,即使其他Callables可能正在等待进行CPU密集型工作。 A few JDBC calls is okay as long as the queries do not take long to complete. 只要查询不需要很长时间才能完成,一些JDBC调用就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当executorService.shutdown(); 应该叫 - When the executorService.shutdown(); should be called ExecutorService.shutDown()和ExecutorService.shutDownNow()之间的中间地带 - Middle ground between ExecutorService.shutDown() and ExecutorService.shutDownNow() 我应该叫`executorService.shutdown`和`executorService.awaitTermination`吗? - should I call `executorService.shutdown` and `executorService.awaitTermination`? 为什么将来的.isDone()必须在executorService.shutdown()之后 - Why future .isDone() must after the executorService.shutdown() 如何在 ExecutorService.shutdown() 之后立即运行未完成的任务? - How to run outstanding tasks immediately after ExecutorService.shutdown()? 从不同于创建ExecutorService的线程中调用ExecutorService.shutdown()和awaitTermination()是否安全? - Is it safe to call ExecutorService.shutdown() and awaitTermination() from a different thread than created the ExecutorService? 在 ExecutorService 上调用 shutdown() 的原因 - Reason for calling shutdown() on ExecutorService 关机还是不关机? 在ExecutorService(Java8)中 - Shutdown or Not Shutdown? in ExecutorService (Java8) Java关闭钩子与ExecutorService - Java shutdown hooks with ExecutorService Java并发-在调用`ExecutorService#execute`之前添加一个Shutdown挂钩 - Java Concurrency - Adding a Shutdown hook before calling `ExecutorService#execute`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM