Java是否提供一个ExecutorService来允许工作程序在同一线程上执行? [英] Does Java provide an ExecutorService which allows a worker to execute on the same thread?

查看:118
本文介绍了Java是否提供一个ExecutorService来允许工作程序在同一线程上执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 ExecutorService 的实现,它将提供以下语义。每个线程都由一个工人占用,该工人根据输入执行某些任务。保证每个工作程序只能在单个线程中执行,因此,应该允许它维护任务与任务之间的状态,而没有同步开销,因为它将在单个线程中与自己同步。

I am looking for an implementation of ExecutorService which will provide the following semantics. Each thread is occupied by a 'worker' that performs some task based on an input. Each worker is guaranteed to only execute in a single thread, thus, it should be allowed to maintain state from task to task, without the overhead of synchronisation, since it would be synchronising with itself, in a single thread.

因此,假设我有100个输入和10个工人,我希望能够编写如下内容:

So let's say I have 100 inputs, and 10 workers, I would like to be able to write something like:

for (Input input: inputs) {
    // The following code would pass t to all 10 workers,
    // each bound to their own thread,
    // and wait for them to complete.
    executor.invokeAll(input);
}

请注意,每个工人对任何给定的输入都会做不同的事情。输入的内容不是可运行的代码块,而只是工作人员的参数。每个工作人员决定如何处理输入。不过,为了简化起见,工作人员实现了一个接口,该接口允许多态调用它,并接收输入。

Note that each Worker does a different thing with any given input. The input is not a runnable block of code, it's just a parameter to the worker. Each worker decides what to do with the input. Though, to make it simpler, the workers implement an interface that would allow it to be called polymorphically, receiving the input.

我用一个 Map< Worker,WorkerExecutor> ,其中 WorkerExecutor 是我围绕 Executors.newSingleThreadPool的薄包装,并且每个线程池中将只运行一个Worker实例。我宁愿找一个知道他们在做什么的人写的东西:-)

I have hacked together something which works, using a Map<Worker, WorkerExecutor>, where WorkerExecutor is my thin wrapper around a Executors.newSingleThreadPool, and only a single instance of Worker will run in each thread pool. I'd prefer to find something written by someone who knows what they're doing :-)

我意识到这种语义会导致效率低下,但是,我试图在开发时间上最大程度地发挥作用,并且将Worker的每个实现重新设计为线程安全的并非易事。我的意思是效率低下,执行可能/将看起来像这样(在此示例中最多模拟2个活动线程):

I realise this kind of semantics will result in inefficiency, however, I'm trying to get the most bang for my buck in terms of development time, and redesigning each implementation of Worker to be thread safe is non-trivial. The inefficiency I mean is that execution could/will look something like this (simulating max 2 active threads for this example):

         | Task 1    | Task 2    | Task 3    | Task 4    |
Worker 1 | =@        | =@        | =@        | =@        |
Worker 2 | ==@       | ==@       | ==@       | ==@       |
Worker 3 |   ==@     |   ==@     |   ==@     |   ==@     |
Worker 4 |    =====@ |    =====@ |    =====@ |    =====@ |

问题在于,Worker 3完成后,没有任务要做,没有工作可以直到工作者4完成为止。

The problem being that after Worker 3 completes, there is no tasks left to do, and no work can be done until Worker 4 completes. That could be arbitrarily long amount of time that a CPU can be left idle.

推荐答案

听起来您实际上想要的是演员。简而言之,actor是一个在单个线程中运行的对象,并且具有负责顺序处理的任务的邮箱。 Akka 似乎是当前领先的库/框架,为JVM提供了参与者。看那边。

It sounds like what you actually want are actors. Put simply, an actor is an object that runs in a single thread and has a "mailbox" of tasks that it's responsible for processing sequentially. Akka seems to be the current leading library/framework providing actors on the JVM. Take a look over there.

这篇关于Java是否提供一个ExecutorService来允许工作程序在同一线程上执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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