两个ExecutorServices是否可以共享一个线程池? [英] Is it possible for two ExecutorServices to share a thread pool?
问题描述
我有一组记录要处理,并且处理可以并行化,所以我创建了一个 ExecutorService (通过 Executors#newCachedThreadPool())。处理单个记录本身是由可并行化的步骤组成的,所以我想使用另一个 ExecutorService
。有没有一个简单的方法使这个新的使用相同的底层线程池?是否甚至更好?感谢。
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.
推荐答案
要回答您的问题:没有,两个ExecutorService对象强>共享线程池。但是,您可以在对象之间共享ExecutorService,或者根据需要创建多个Executor,尽管这不太推荐。
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.
最佳解决方案:在对象之间共享Executor。
Best solution: share the Executor between your objects.
这篇关于两个ExecutorServices是否可以共享一个线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!