What do you understand by Callable and Future in Java?

Answer

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.

All concurrency Questions

Ask your interview questions on concurrency

Write Your comment or Questions if you want the answers on concurrency from concurrency Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---