Difference Between Callable and Runnable in Java
In this tutorial, we are going to learn about the differences between callable and runnable and in Java. java.lang.An interface designated as "runnable" is one that a class whose instances are meant to be run by a thread must implement. Subclassing Thread and implementing Runnable are the two methods available for creating a new thread. When a task can be completed by overriding just the run() function of Runnable, there is no need to subclass Thread.
Tasks intended to be carried out by a separate thread are encapsulated under Callable and Runnable interfaces.
On the other hand, Callable instances can only be executed by ExecutorService, whereas Runnable instances can be run by both Thread class and ExecutorService.
Callable interface:
Essentially, a callable interface throws a checked exception and returns some data. One of the main distinctions between this and the future Runnable interface is that no value is returned. This interface does nothing more than compute a result and, if it is unable to do so, throw an exception.
public interface Callable{ V call() throws exception ; }
- The package "java.util.concurrent" contains its declaration.
- Additionally, this interface has a single call() function that accepts no arguments.
- By passing callable as an argument, we are unable to start a thread.
- Callable has the ability to return outcomes. The "throws Exception" clause in the call() method of Callable allows us to easily propagate checked exceptions farther.
Filename: CallableInterface.java
import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; // A class that uses the Callable interface class CallableMsg implements Callable{ public String call() throws Exception { return "Hello World!"; } } public class CallableInterface { static ExecutorService service = Executors.newFixedThreadPool(2); public static void main(String[] args) throws Exception { CallableMsg task = new CallableMsg(); Future futureMessage = service.submit(callableTask); System.out.println(futureMessage.get().toString()); } }
Output:
Hello world
Runnable interface:
When a thread is created using an object that implements this interface, the object run function is called in a thread that is operating separately when the thread is started. This run() method can do anything at all, according to its general contract.
- java.lang.Runnable is an interface that defines the run() function as its only method.
- It symbolizes a Java task that is carried out by a thread.
- Implementing the Runnable interface or subclassing the Thread class are the two methods available to you when using Runnable to create a new thread.
- Runnable is not able to throw checked exceptions or return the computation result, which is necessary if you are doing some computation in a separate thread.
Filename: RunnableInterface
import java.io.FileNotFoundException; import java.util.concurrent.*; // An object type utilizing the Runnable interface class RunnableImpl implements Runnable { public void run() { System.out.println("Hello World from a different thread than Main"); } } public class RunnableInterface { static ExecutorService service = Executors.newFixedThreadPool(2); public static void main(String[] args) { // Using the Thread class, create and execute a runnable job RunnableImplements runnableTask = new RunnableImplements(); Thread thread = new Thread(runnableTask); thread.start(); // using Executor Service to create and execute runnable tasks. service.submit(runnableTask); } }
Output:
Hello World from a different thread than Main Hello World from a different thread than Main
Differences between callable and runnable
Runnable Interface | Callable interface |
Since Java1.0, it has been a component of the java.lang package. | Since Java 1.5, it has been a component of the java.util.concurrent package. |
It is unable to return the computation's outcome. | It has the ability to return the outcome of a task's parallel processing. |
It is unable to throw a validated exception. | Throwing a checked Exception is possible. |
The Java run() function must be overridden in a runnable interface. | We must override the call() method in order to use Callable. |