I’m currently thinking about using Akka (Java API/libraries) to accomplish the task of creating several Futures 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 Futures and sometimes not.
best regards,
Johannes
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.