views:

585

answers:

3

I've got a collection of records to process, and the processing can be parallelized, so I've created an ExecutorService (via Executors#newCachedThreadPool())). The processing of an individual record is, itself, composed of parallelizable steps, so I'd like to use another ExecutorService. Is there an easy way to make this new one use the same underlying thread pool? Is it even desirable? Thanks.

+2  A: 

Can you just pass a reference to the existing ExecutorService to your worker objects?

public class Task implements Runnable
{
    private final ExecutorService threadPool;
    private final SubTask[] subtasks;

    public Task(ExecutorService threadPool)
    {
        this.threadPool = threadPool;
        this.subtasks = createSubtasksIGuess();
    }

    public void run()
    {
        for(SubTask sub : subtasks)
            threadPool.submit(sub);
    }
}
Outlaw Programmer
+6  A: 

To answer your question: no, two ExecutorService objects cannot share a thread pool. However you can share an ExecutorService between your objects, or alternatively create several Executors, as necessary, though this is less recommended.

Best solution: share the Executor between your objects.

Yuval A
+5  A: 

Short answer: No.

Longer answer: You will need your own implementation to do that. ExecutorService is an interface and AbstractExecutorService is quite easy to implement. If you want two ExecutorService sharing same ThreadPool (e.g. with different maximum active thread value), you may use proxy pattern to make ThreadPool sharing ExecutorService.

Dennis Cheung
Thank you! The proxy pattern would be beautiful here. Duplicating Doug Lea's code for a pluggable thread pool would have been too error-prone.
Michael Deardeuff
(My use case was I needed to wait until all the tasks finished per Executor. That is, use the shutdown()/awaitTermination() methods)
Michael Deardeuff