
Runnable and Callable are both useful for implementing multithreading. Runnable has been enhanced into Callable in Java 1.5. It’s important to understand that Tasks that will be executed by other threads are encapsulated using the Callable and Runnable interfaces.
The java.util.concurrent package has a Callable. Runnable is available in java.lang.Thread. There are, however, some differences between these interfaces.
What is Callable in Java? Callable is a Java interface that allows you to run many processes in parallel.
Table of Contents
Runnable Vs Callable interface in Java
When using Executor Framework, the topic of Runnable vs Callable arises. The key difference between Runnable and Callable is that Callable returns to the caller the outcome of running a job. You can’t return anything if you use Runnable.
# | Callable | Runnable |
---|---|---|
1. | Callable was first introduced in Java 1.5. | Runnable was first introduced in Java 1.0. |
2. | Callable has return type it means Callable can return value. | Runnable does not return value. |
3. | Callable has call() method. | Runnable has run() method. |
4. | Callable throws CheckedException. | Runnable cannot throw CheckedException. |
5. | Callable is a generic interface. | But Runnable is not a generic interface. |
6. | public interface Callable<V> { V call() throws Exception; } | public interface Runnable { void run(); } |
7. | The Callable interface cannot be passed to a thread as a parameter to create a new thread. | But we can pass Runnable to a thread as a parameter to create a new thread. |
We can only use Callable in the Executor Framework. However, Runnable may be handed to a single thread for execution as well as the Executor framework.
How to convert Runnable to Callable?
You can use the Executor class’s utility method for conversion.
Runnable runnable = ...; Callable callable = Executors.callable(runnable);
Note: If you’re familiar with Executors, Callable is a better choice than Runnable.
Example of Runnable interface in java
You may start a new thread with Runnable by either implementing the Runnable interface or subclassing the Thread class. The Runnable interface is demonstrated in the example below.
public class RunnableExample implements Runnable {
public void run() {
System.out.println("Runnable Example!");
}
public static void main(String args[]) {
RunnableExample r = new RunnableExample();
Thread t = new Thread(r);
t.start();
}
}
Example of Callable interface in java
The Callable interface is demonstrated in the example below.
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
class CallableExample implements Callable {
@Override
public String call() throws Exception {
System.out.println("In Call Method");
String name = "This is the example of Callable";
return name;
}
}
public class CallableDemo {
public static void main(String args[]) throws Exception {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future f = executorService.submit(new CallableExample());
System.out.println("Note: " + f.get());
}
}
See the difference between Runnable and Thread
Interview Questions and Answers Runnable Vs Callable
If you have tasks that you want to schedule and forget, use Runnable. Use a Callable if you want to get a value from a method.
Yes, Runnable is a function interface.
The Thread class does not have a method that accepts the Callable interface.
No
No, It is not true. We can’t create a thread with a Callable.
Yes, Callable is a functional interface.
Use the Callable interface to encapsulate a task and send it to a thread or thread pool for asynchronous execution.
You can’t return any value from Runnable. Runnable returns void type.
Yes
Runnable is recommended.