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.
Jane wayne
source share