Call ExecutorService.shutDown () in java

I am starting to learn the class ExecutorService. The documentation (and online tutorials) says that you should always call ExecutorService.shutDown () to restore resources. however, the documentation also says that after calling shutDown (), new tasks will not be accepted. therefore, to my question, should I always instantiate a new ExecutorService whenever I need to parallelize data processing?

right now i have a list of called objects and i am doing 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 does data processing in parallel using 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 whether it was for the ExecutorService or the Fork / Join approach, but I remember the documentation and tutorials that said that the actual parallel procedure for data processing should not include I / O operations and everything should be done in memory. however, in my InnerCallable, I actually make JDBC calls (not shown here).

Ultimately, I am using ExecutorService, but I still have problems.

  • my approach is above good programming practice using ExecutorService?
  • Should I use a single instance of ExecutorService?
  • Should I not only avoid I / O operations inside my parallel methods, but also JDBC calls?

as a last problem, I tried to research a little Fork / Join vs ExecutorService. I came across an article that completely exploded the Fork / Join APIs / classes. Is it worth learning Fork / Join? I saw several articles about stackoverflow and other places where tests are used to compare Fork / Join vs ExecutorService, and there are graphs showing the best processor usage for Fork / Join vs ExecutorService (via the Windows task manager). however, when I use ExecutorService (JDK 1.7.x), my CPU usage is max. has the ExecutorService improved with the latest JDK?

any help / guidance appreciated.

+7
source share
1 answer

You must add awaitTermination calls because shutDown returns without waiting for Callables to complete. Besides,

  • Do you have OuterCallable sequential dependencies? If so, your approach is fine, but using ForkJoinPool is preferable because it will maintain a low number of worker threads. Otherwise, it would be better to send a large flattened Callable collection to one Executor .
  • Only if you want to use ExecutorService in several different procedures and want to avoid passing it. If it is used only in someMethod , it can also create it where you do.
  • You should avoid I / O procedures that are time consuming. If your ExecutorService has 4 worker threads and they all block I / O, the JVM will not use the processor at all, even if other Callables can wait for the processor to work. Multiple JDBC calls are fine if the requests do not take a lot of time.
+4
source

All Articles