本文介绍了是否有一个具有多个队列(确保每个队列的串行处理)的开箱即用的线程池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的所有任务中,我有一些必须连续处理(它们永远不能同时运行,必须按顺序处理)。

Among all my tasks, I have some that must be processed serially (they can never run concurrently and they must be processed in order).

对于必须串行执行的每组任务,具有单个线程的分离的线程池。它工作,但我没有资源。我不控制组的数量,所以我可能最终与一个可笑的数量的线程同时运行。

I achieved that creating a separated thread pool with a single thread for each group of tasks that must be executed serially. It works but I don't have the resources for that. I don't control the number of groups, so I might end up with a ridiculous number of threads running simultaneously.

有任何方式,我可以用一个单一的线程池?是否有一个线程池有多个阻塞队列,我可以确保每个队列的串行执行?

Is there any way I can accomplish that with a single thread pool? Is there a thread pool with multiple blocking queues where I could ensure serial execution for each queue?

只是强调我在第二段中说的:我已经解决了这个问题,使用一个单线程线程池为每一组任务必须连续执行。我不能继续使用这个解决方案,虽然。有太多的组,我不能有所有这些线程。

Just emphasizing what I've said in my second paragraph: I've solved this with a single threaded thread pool for each group of tasks that must be executed serially. I can't go on with this solution, though. There are way too many groups and I can't have all these threads.

我发现了这个相关的问题,但由于它不是很近,我仍然创建矿。我所做的是试图避免重复发明轮,但似乎我没有选择。

I've found this related question, but since it is not very recent, I still created mine. All I'm doing is trying to avoid reinventing the wheel, but it seems I don't have a choice.

,由@SotiriosDelimanolis和@AlexeiKaigorodov建议有希望,以及,这当然解决了问题。唯一的缺点是,我必须编写自己的轮询策略,以确保我的任务最终添加到执行器(如在他的例子中的无限循环)。

Akka, as suggested by @SotiriosDelimanolis and @AlexeiKaigorodov seems promising, as well as @Dodd10x second answer, which certainly solves the problem. The only downside is that I'd have to code my own polling strategy to make sure my tasks are eventually added to the executor (like the infinite loop in his example).

另一方面,@OldCurmudgeon建议的与我的问题完全匹配,只需作为一个自定义 ExecutorService

On the other hand, the Striped Executor Service suggested by @OldCurmudgeon exactly matches my problem and works out of the box simply as a custom ExecutorService.

甚至有一个关于每个组(条带)使用单线程线程池的注释,如下所示:

There is even a comment about using a single threaded thread pool for each group (stripe), as it was suggested here:

我认为这是最简单的解决方案和易用性。

I see this as the best solution for its simplicity and ease of use.

这篇关于是否有一个具有多个队列(确保每个队列的串行处理)的开箱即用的线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 12:19