"The future depends on what you do today" M. Gandhi
Future is java interface: public interface Future<V>
A Future represents the result of an asynchronous computation.Methods of Future interface are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The class that implements Future might look as follows
Methods of Future:
cancel() - attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason.
isDone() - Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true
get() - Waits if necessary for the computation to complete, and then retrieves its result.
get(long timeout, TimeUnit unit) - Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available
Thus, method ‘get’ is a key method: it blocks current thread (with/without timeout) until the operation of another thread is completed.
Mostly we don’t need to define custom implementations of Future because Java library provides a bunch of various built-in Future implementations. For example, FutureTask – it is a commonly used implementation of the Future interface.
l FutureTask implements Runnable interface (so the FutureTask instance could be called by Executor as well as be called directly).
l FutureTask may take Runnable as constructor parameter;
l Future task may take Callable as constructor parameter.
All these advantages mean that we have a flexible and generic execution mechanism for our concurrent tasks, wrapping Runnable/Callable instance with FutureTask we get the job which can be scheduled, cancelled and the job result is available via Future methods ‘get’
There is some basic scenario of using FutureTask