Why can't i instantiate ThreadPoolExecutor with BlockingQueue<Callable>; why only BlockingQueue<Runnable>?

StackOverflow https://stackoverflow.com/questions/18153496

  •  24-06-2022
  •  | 
  •  

Question

My understanding is that callable was added in 1.5 and the runnable interface was kept as-is to prevent the world from ending. Why can't I instantiate a ThreadPoolExecutor(core, max, tu, unit, new BlockingQueue<Callable>()) - why does the queue necessarily take runnable only? Internally, if i were to submit, invokeAll, invokeAny callables, this should be fine right? Also, would shutDownNow() return a list of callables?

Was it helpful?

Solution

You can submit Callables, but they get wrapped internally as Runnables (actually FutureTasks, which implement Runnable). shutDownNow() is only going to return Runnables, just like it says on the tin.

If you want to get the list of Callables that haven't been run, you'll need to keep track of them yourself somehow (e.g., keep a list of them and make them responsible for removing themselves from the list when they're called.)

OTHER TIPS

As a more general answer, you can't influence the runtime behavior of a Java program by changing a type parameter. No if branch can be re-routed based on type parameters. If you ever find yourself in a situation where you wish to get a different behavior from an API, never look for the solution in the choice of type parameters.

In this particular case, a Runnable is a more generic type of object: it is a unit of work that the internals of the Executor can submit to a thread. For example, the Runnable can contain code which calls a Callable and saves its result somewhere.

It can't take both BlockingQueue<Callable<T>> and BlockingQueue<Runnable> due to type erasure; both overloads would have the same raw type BlockingQueue, and so would conflict.

I don't know what you would do with a list of Callables submited to the executor, though. What would you do with their result? Where would it go?

It sounds like you want a Future<T>. You can submit a collection of Callable<T>s using invokeAll, and you'll get back a collection of Future<T>s that you can get values from once they're available.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top