Calling ExecutorService. in Java shutDown()
I began to learn the executorservice class Documentation (and online tutorials) says executorservice is always called Shutdown() to recycle resources However, the document also says that it will not accept any new tasks after calling shutdown () So, my question is, whenever I need to parallelize data processing, do I always have to instantiate a new executorservice?
Now that I have a list of callable objects, 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 uses innercallable to split data or perform data processing in parallel
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 don't remember whether it is used for executorservice or fork / join method, but I remember that documents and tutorials say that the actual parallel process of operating data should not involve I / O operations, and everything should be completed in memory However, in my innercallable, I'm actually making a JDBC call (not shown here)
Finally, the way I use executorservice works, but I still have some lingering concerns
>Is my approach to good programming practices using executorservice? > Should I use a singleton instance of executorservice? > Should I avoid not only I / O operations in parallel methods, but also JDBC calls?
As a final question, I try to study fork / join vs executorservice I found an article that completely attacked the fork / join API / class Is it worth learning fork / join? I've seen articles in stackoverflow and elsewhere, where tests are used to compare fork / join and executorservice, and some charts show better CPU utilization of fork / join vs executorservice (through Windows Task Manager) However, when I use executorservice (JDK 1.7. X), my CPU usage is the maximum Has executorservice been improved with the latest JDK?
Appreciate any help / guidance
Solution
You should add the awaittermination call because shutdown returns without waiting for callables to complete besides,
>Are outercallables sequence dependent? If so, your method is good, but it is better to use forkjoinpool because it will keep the number of working threads low If not, it is better to submit a large flattened callables collection to an executor. > Only if you want to use executorservice in several different routines and want to avoid passing it If it is only used in somemethod, it can also be instantiated there like you. > You should avoid I / O routines that take a long time to complete If your executorservice has four worker threads and all of them are blocked on I / O, the JVM will not use CPU at all, even though other callables may be waiting for CPU intensive work As long as the query does not take a long time to complete, some JDBC calls are OK