我目前正在考虑使用 Akka(Java API/库)来完成创建多个 Future
并将它们放入 BlockingQueue
的任务。现在,由 Callables 处理的某些任务可能在调用线程中运行得更快/更快,而不是创建新线程或等待新线程可用。我认为 Akka 正是这样做的,例如,如果我正在运行:
Future<String> f1 = future(new Callable<String>() {
public String call() {
return "Hello" + "World";
}
});
它可能在调用 future(Callable) 的当前线程中执行,还是我错了?也许我错了,因为我不明白调度程序如何决定是否创建新线程。
现在我只是使用 ExecutorService,它使用其他线程,但由于某些任务确实非常快,它们也可以由当前线程处理。但我使用的是 BlockingQueue>
这就是为什么我有时不能使用 Future
有时不能。
最好的问候,
约翰内斯
I'm currently thinking about using Akka (Java API/libraries) to accomplish the task of creating several Future
s and put them into a BlockingQueue
. Now it might be that some tasks which are handled by Callables are fast/faster to run in the calling thread instead of creating a new Thread or waiting for a new Thread to become available. I think Akka is doing exactly this, for example if I'm running:
Future<String> f1 = future(new Callable<String>() {
public String call() {
return "Hello" + "World";
}
});
It might be executed in the current thread which invokes future(Callable) or am I wrong? Maybe I'm wrong because I don't get how the dispatcher would decide if a new Thread is created or not.
Right now I'm just using an ExecutorService which uses other Threads, but since some tasks are really really fast they could as well be handled by the current Thread. But I'm using a BlockingQueue<Future<Float>>
that's why I can't sometimes use Future
s and sometimes not.
best regards,
Johannes
发布评论
评论(1)
它始终使用提供的调度程序中的一个线程,如果没有指定,则使用全局默认值。要使用相同的线程,您需要传入 CallingThreadDispatcher,但它将始终是同步的。除了程序员之外,任何人都无法决定计算是否便宜。
It always uses a thread from the supplied dispatcher, or the global default if no one is specified. To use the same thread you'd need to pass in a CallingThreadDispatcher, but then it will always be synchronous. There is no way at all that anyone except the programmer can decide whether a computation is cheap or not.