简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM