Is it possible for two ExecutorServices to share a thread pool?

谁都会走 提交于 2019-12-21 12:17:15

问题


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.


回答1:


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.




回答2:


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.




回答3:


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);
    }
}



回答4:


There are a few good reasons to want to do this, such as separate bounds and metrics per queue. AFAIK, Cassandra 2.1 gained not insignificantly from using an executor service implementation with a shared thread pool; the code is at https://github.com/apache/cassandra/blob/trunk/src/java/org/apache/cassandra/concurrent/SEPExecutor.java.



来源:https://stackoverflow.com/questions/368613/is-it-possible-for-two-executorservices-to-share-a-thread-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!