Google News
logo
Java Multithreading - Interview Questions
What do you understand by Callable and Future in Java?
Java Callable interface : In Java5 callable interface was provided by the package java.util.concurrent. It is similar to the Runnable interface but it can return a result, and it can throw an Exception. It also provides a run() method for execution of a thread. Java Callable can return any object as it uses Generic.
 
Syntax :
public interface Callable<V>
 
Java Future interface : Java Future interface gives the result of a concurrent process. The Callable interface returns the object of java.util.concurrent.Future.
 
Java Future provides following methods for implementation.
 
cancel(boolean mayInterruptIfRunning) : It is used to cancel the execution of the assigned task.
get() : It waits for the time if execution not completed and then retrieved the result.
isCancelled() : It returns the Boolean value as it returns true if the task was canceled before the completion.
isDone() : It returns true if the job is completed successfully else returns false.
Advertisement