Difference between Runnable and Callable
In Java, both Runnable and Callable are interfaces used to define tasks for execution by a thread or an executor service. However, they have distinct characteristics and use cases.
Table of Contents
Runnable Interface:
- Purpose: The Runnable interface is designed for classes whose instances are intended to be executed by a thread.
- Method: It has a single method void run().
- Return Type: It does not return a result and cannot throw a checked exception.
- Usage: It is used when you do not need to return any result from the task or throw checked exceptions.
Callable Interface
- Purpose: The Callable interface is similar to Runnable, but it is designed to return a result and can throw a checked exception.
- Method: It has a single method V call() throws Exception.
- Return Type: It returns a result of type V.
- Usage: It is used when you need to return a result from the task or handle checked exceptions.
Example
Implementing Runnable
java
class MyRunnable implements Runnable {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Runnable Thread ID: " + Thread.currentThread().getId() + " Value: " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class RunnableExample {
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable());
t1.start();
}
}
Implementing Callable
java
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
class MyCallable implements Callable<Integer> {
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
System.out.println("Callable Thread ID: " + Thread.currentThread().getId() + " Value: " + i);
Thread.sleep(500); // Sleep for 500 milliseconds
}
return sum;
}
}
public class CallableExample {
public static void main(String[] args) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Integer> future = executor.submit(new MyCallable());
try {
System.out.println("Sum: " + future.get());
} catch (InterruptedException | ExecutionException e) {
System.out.println(e);
} finally {
executor.shutdown();
}
}
}
Explanation Runnable and Callable
1. Using Runnable
- The Runnable interface is implemented by the MyRunnable class.
- The run() method contains the code that will be executed by the thread.
- A Thread object is created with the MyRunnable instance and started using start().
2. Using Callable:
- The Callable interface is implemented by the MyCallable class.
- The call() method contains the code that will be executed by the thread and returns an Integer result.
- An ExecutorService is used to manage the thread lifecycle.
- The submit() method is used to execute the Callable task, and a Future object is returned.
- The result of the computation is retrieved using the get() method of the Future object.