Home > other >  Why does submitting a Task to an Executor result in a Future<*>
Why does submitting a Task to an Executor result in a Future<*>

Time:07-23

Let's say I have a Task class

class Abc : Task<Foo>() {
   ...
}

and a Callable class

class Def : Task<Foo>() {
   ...
}

When I try to run the Callable and get a future Executors.newSingleThreadExecutor().submit(Def()) I get a Future<Foo>. However, when I try to run the Task Executors.newSingleThreadExecutor().submit(Abc()) I get Future<*>.

How can I get a future of the correct type using the JavaFX Task class? Or should I be doing something entirely different?

CodePudding user response:

The Task class (assuming it is this class) only implements the Runnable interface for the ExecutorService. Because the Runnable has no return type the returned Future can't have a result type. For the Callable interface the ExecutorService has a separate method that returns a future using the return type of the Callable instance.

What you could to is to create a callable instance that executes the task impelementation instead of passing the task instance to the ExecutorService

  • Related