Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Future and FutureTask in Java?

Since use ExecutorService can submit a Callable task and return a Future, why need to use FutureTask to wrap Callable task and use the method execute? I feel they both do the same thing.

like image 458
Hesey Avatar asked Feb 10 '11 12:02

Hesey


People also ask

What is FutureTask in Java?

A FutureTask can be used to wrap a Callable or Runnable object. Because FutureTask implements Runnable , a FutureTask can be submitted to an Executor for execution. In addition to serving as a standalone class, this class provides protected functionality that may be useful when creating customized task classes.

What is Future & difference between runnable and Callable?

Callable interface and Runnable interface are used to encapsulate tasks supposed to be executed by another thread. However, Runnable instances can be run by Thread class as well as ExecutorService but Callable instances can only be executed via ExecutorService.

Does Future get () block?

A Future interface provides methods to check if the computation is complete, to wait for its completion and to retrieve the results of the computation. The result is retrieved using Future's get() method when the computation has completed, and it blocks until it is completed.

What is Callable and Future in Java?

The Callable object returns a Future object which provides methods to monitor the progress of a task being executed by a thread. The future object can be used to check the status of a Callable and then retrieve the result from the Callable once the thread is done. It also provides timeout functionality.


1 Answers

FutureTask This class provides a base implementation of Future, with methods to start and cancel a computation

Future is the interface

like image 156
fmucar Avatar answered Sep 23 '22 05:09

fmucar