运行 ScheduledThreadPoolExecutor 的两个实例

发布于 2024-12-26 02:29:58 字数 280 浏览 2 评论 0原文

我有许多异步任务要并行运行。所有任务都可以分为两种类型,我们称之为 A 型(耗时)和 B 型(执行速度更快)。 使用具有x池大小的单个ScheduledThreadPoolExecutor,最终在某个时刻所有线程都忙于执行类型A,因为结果类型B被阻塞和延迟。 我想要完成的是与类型 B 并行运行类型 A 任务,并且我希望这两种类型的任务在其组内并行运行以提高性能。

您认为为 A 类型和 B 类型拥有两个专门拥有自己的线程池的 ScheduledThreadPoolExecutor 实例是否明智?您认为这种方法有什么问题吗?

I have a number of asynchronous tasks to run in parallel. All the tasks can be divided into two types, lets call one - type A (that are time consuming) and everything else type B (faster and quick to execute ones).
with a single ScheduledThreadPoolExecutor with x poolsize, eventually at some point all threads are busy executing type A, as a resul type B gets blocked and delayed.
what im trying to accomplish is to run a type A tasks parallel to type B, and i want tasks in both the types to run parallel within their group for performance .

Would you think its prudent to have two instances of ScheduledThreadPoolExecutor for the type A and B exclusively with their own thread pools ? Do you see any issues with this approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2025-01-02 02:29:58

不,这似乎很合理。
我正在做类似的事情,即我需要根据某个 id 以串行方式执行任务,例如 id="1" 组件的所有任务都需要彼此串行执行,并且与用于的所有其他任务并行执行具有不同 id 的组件。
所以基本上我需要为每个不同的组件一个单独的任务队列,这些任务是从每个特定队列中一个接一个地拉出的。
为了实现这一点,我

Executors.newSingleThreadExecutor(new JobThreadFactory(componentId));

对每个组件都使用了它。
另外,我需要 ExecutorService 来执行不绑定到 componentIds 的不同类型的任务,为此我创建了额外的 ExecutorService 实例,

Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE, new JobThreadFactory());

这至少适合我的情况。
我能想到的唯一问题是否需要有序执行任务,即
task2 需要在 task1 之后执行,依此类推...但我怀疑这里的情况...

No, that's seems reasonable.
I am doing something similar i.e. I need to execute tasks in serial fashion depending on some id e.g. all the tasks which are for component with id="1" need to be executed serially to each another and in parallel to all other tasks which are for components with different ids.
so basically I need a separate queue of tasks for each different component, the tasks are pulled one after another from each specific queue.
In order to achieve that I use

Executors.newSingleThreadExecutor(new JobThreadFactory(componentId));

for each component.
Additionally I need ExecutorService for a different type of tasks which are not bound to componentIds, for that I create additional ExecutorService instance

Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE, new JobThreadFactory());

This works fine for my case at least.
The only problem I can think of if there is a need of ordered execution of the tasks i.e.
task2 NEEDS to be executed after task1 and so on... But I doubt this the case here ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文