ExecutorService,按顺序执行任务但从池中获取线程 [英] ExecutorService that executes tasks sequentially but takes threads from a pool

查看:1448
本文介绍了ExecutorService,按顺序执行任务但从池中获取线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建 ExecutorService 的实现,让我们称之为 SequentialPooledExecutor ,具有以下属性。

I am trying to build an implementation of the ExecutorService, let's call it SequentialPooledExecutor, with the following properties.


  1. SequentialPooledExecutor 的所有实例共享相同的线程池

  1. All instances of SequentialPooledExecutor share the same thread pool

按顺序执行对 SequentialPooledExecutor 的同一实例的调用。

Calls on the same instance of SequentialPooledExecutor are executed sequentially.

换句话说,实例在开始处理其下一个任务之前等待当前正在执行的任务的终止排队。

In other words, the instance waits for the termination of the currently executing task before start processing the next task in its queue.

我目前正在实施 SequentialPooledExecutor 我自己,但我想知道我是否正在重新发明轮。我研究了 ExecutorService 的不同实现,例如 Executors 类提供的那些,但是我找不到符合我要求的那个。

I am currently in the process of implementing the SequentialPooledExecutor myself, but I am wondering if I am reinventing the wheel. I looked into different implementations of ExecutorService, for example those that are provided by the Executors class, but I did not find one that meets my requirements.

你知道我现有的实现是否缺失,还是我自己应该继续实现接口?

Do you know whether there are existing implementations that I am missing, or should I continue with implementing interface myself?

编辑:

我认为我的要求不是很清楚,让我看看我是否能用其他解释单词。

I think my requirement are not very clear, let's see if I can explain it with other words.

假设我有一系列会话,比如1000个(我之前称之为执行程序实例的东西)。我可以向会话提交任务,我希望保证提交给同一会话的所有任务都按顺序执行。但是,属于不同会话的任务应该彼此没有依赖关系。

Suppose I have a series of sessions, say 1000 of them (the things that I was calling instance of the executor before). I can submit tasks to a session and I want the guarantee that all tasks that are submitted to the same session are executed sequentially. However, tasks that belong to different sessions should have no dependency from each other.

我想定义一个 ExecutorService 执行这些任务,但使用有限数量的线程,比方说200,但确保在同一个会话中的前一个任务完成之前没有启动任务。

I want to define an ExecutorService that executes these tasks, but uses a bounded number of threads, let's say 200, but ensures that a task is not started before the previous one in the same session is finished.

我不知道是否有任何已经存在的东西,或者我是否应该自己实现这样的 ExecutorService

I don't know if there is anything existing that already does that, or if I should implement such an ExecutorService myself.

推荐答案

如果要按顺序执行任务,只需创建 ExecutorService 只有一个线程,这要归功于 Executors.newSingleThreadExecutor()

If you want to execute your task sequentially simply create a ExecutorService with only one thread thanks to Executors.newSingleThreadExecutor().

我如果您有不同类型的任务,并且您只想按顺序执行相同类型的任务,则可以使用相同的单线程 ExecutorService 相同类型的任务,无需重新发明轮子。

If you have different type of tasks and you want to execute only the tasks of the same type sequentially, you can use the same single threaded ExecutorService for the same type of tasks, no need to reinvent the wheel.

因此,假设你有 1 000 不同类型的任务,你可以使用 200 单线程 ExecutorService ,你需要自己实现的唯一事情是你总是需要使用相同的单线程<$ c对于给定类型的任务,$ c> ExecutorService 。

So let's say that you have 1 000 different type of tasks, you could use 200 single threaded ExecutorService, the only thing that you need to implement yourself is the fact that you always need to use the same single threaded ExecutorService for a given type of task.

这篇关于ExecutorService,按顺序执行任务但从池中获取线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆