last modified: 21-feb-2017 (21:45)
A Callable is a generic interface, represents a thread which will return a value.
It has a single method
call() which throws
Exception and returns an object of given type.
Callable is similar to
Runnable with two major differences.
Runnable task can not return a value while a
Callable can throw an exception while a
Runnable can not.
Callablemust return a value if task is completed successfully or in case of failure an exception must be thrown.
ExecutorService#submit method accepts objects of type callable and execute them in their own threads.
Future object can be queried for getting information about task cancellation or completion. The most important method of
Future object is
get() which returns the result of
get() is a blocking a call and it stops the execution of current thread unless one of the following event occurs
Current thread is interrupted (
InterruptedException is thrown).
Task is canceled (
CancellationException is thrown).