Javatpoint Logo
Javatpoint Logo

Java Callable Example

Java provides two approaches for creating threads one by implementing the Runnable interface and the other by inheriting the Thread class. However, one important feature missing with the implementation of the Runnable interface is that it is not possible for a thread to return something when it completes its execution, i.e., when the run() method execution is over. In order to support this feature, the Java Callable interface is used.

To understand the concept of callable, one must friendly with threads and multithreading concepts.

The call() Method Signature

Callable Examples

Let's observe the code snippet which implements the Callable interface and returns a random number ranging from 0 to 9 after making a delay between 0 to 4 seconds.

FileName: JavaCallableExample.java

Explanation: The above code snippet shows how one can implement the callable interface. However, the job is not done. When the call() method terminates, the returned object must be stored that is known to the main thread. It is important because the main thread must know the result generated in the call() method. To accomplish the same, a Future object is used. A Future object holds the result obtained from the different thread, which is sent from the call() method.

Just like Callable, Future is also an interface. Therefore, to use it, its implementation is a must. However, we do not have to take the pain to implement the Future interface. The Java library already has the class called, FutureTask that implements the Runnable as well as the Future interfaces.

Now, let's extend the above code snippet.

FileName: JavaCallableExample.java

Output:

The random number is: 5
The random number is: 0
The random number is: 1
The random number is: 3
The random number is: 9
The random number is: 0
The random number is: 7
The random number is: 9
The random number is: 3
The random number is: 5

Explanation: In the code, we have produced 10 different threads. Each thread invokes the call() method, generates a random number, and returns it. The get() method is used to receive the returned random number object obtained from the different threads to the main thread. The get() method is declared in the Future interface and implemented in the FutureTask class.

Differences between Callable and Runnable Interface

The following table shows the differences between the Callable and Runnable interfaces.

Callable Runnable
It is mandatory to define call() method if we are dealing with the Callable interface. It is mandatory to define the run() method if we are dealing with the Runnable interface.
The return type of the call() method of the interface is an Object. Hence, the call() method returns an Object. The return type of the run() method of the interface is void. Hence, the run() method returns void.
The call() method can throw an exception. The run() method cannot throw an exception.
A thread cannot be created using the Callable interface. A thread can be created using the Runnable interface.

Next TopicBlockchain Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA