Both Callable and Runnable are interfaces for describing tasks that can be executed simultaneously, usually by threads. Here is the main difference between Callable and Runnable.
Return Value
Runnable- The run() method of the Runnable interface does not return a result. A null method, means that it executes an event but returns no value.
Callable- The call() method of the Callable interface returns a result of the specified type (specified by the generic type parameter <V>). The
call() method can throw checked exceptions, which must be handled or thrown or declared.
Handling Exception
Runnable- the run() method in Runnable cannot directly throw a noted exception. If it needs to throw a checked exception, it should be caught in the
run() method and handled accordingly.
Callable- the call() method in Callable can throw a flagged exception. These exceptions must be caught in the
call() method or declared in the throws clause of the method.
Usage with ExecutorService
Both the Runnable and the Callable can be passed to the ExecutorService for execution at the same time.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class RunnableExample {
public static void main(String[] args) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
// Runnable task
Runnable task = () -> {
try {
Thread.sleep(2000); // Simulate some task
System.out.println("Runnable task completed");
} catch (InterruptedException e) {
e.printStackTrace();
}
};
// Submit the task to the executor service
executorService.submit(task);
// Shutdown the executor service after task completion
executorService.shutdown();
}
}
Callable Syntax-
Callable<Integer> task = () -> {
// Task implementation that returns a result
return someResult;
};
Future<Integer> future = executorService.submit(task);
Example-
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallableExample {
public static void main(String[] args) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
// Callable task
Callable<Integer> task = () -> {
Thread.sleep(3000); // Simulate some time-consuming task
return 42; // Return a result
};
// Submit the task to the executor service
Future<Integer> future = executorService.submit(task);
try {
// Wait for the task to complete and get the result
Integer result = future.get();
System.out.println("Callable task completed, result: " + result);
} catch (Exception e) {
e.printStackTrace();
}
// Shutdown the executor service after task completion
executorService.shutdown();
}
}
Return Value Handling
Runnable- Since Runnable doesn’t return a value, you typically use a separate tool (such as a shared variable or some other synchronization technique) if you need to get a result or condition after a task is complete
Callable- When a Callable completes its execution, it returns a Future object that can be used to retrieve a statistic or to handle exceptions thrown during its execution.
Callable is an improvement over Runnable that lets functions return values and throw checked exceptions, providing greater flexibility in concurrent programming scenarios
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Difference Between Callable and Runnable
Both Callable and Runnable are interfaces for describing tasks that can be executed simultaneously, usually by threads. Here is the main difference between Callable and Runnable.
Return Value
Runnable- The
run()method of the Runnable interface does not return a result. A null method, means that it executes an event but returns no value.Callable- The
call()method of the Callable interface returns a result of the specified type (specified by the generic type parameter<V>). Thecall()method can throw checked exceptions, which must be handled or thrown or declared.Handling Exception
Runnable- the
run()method in Runnable cannot directly throw a noted exception. If it needs to throw a checked exception, it should be caught in therun() method and handled accordingly.Callable- the
call()method in Callable can throw a flagged exception. These exceptions must be caught in thecall()method or declared in thethrowsclause of the method.Usage with ExecutorService
Both the Runnable and the Callable can be passed to the ExecutorService for execution at the same time.
Example-
Runnable Syntax-
Example-
Callable Syntax-
Example-
Return Value Handling
Runnable- Since Runnable doesn’t return a value, you typically use a separate tool (such as a shared variable or some other synchronization technique) if you need to get a result or condition after a task is complete
Callable- When a Callable completes its execution, it returns a Future object that can be used to retrieve a statistic or to handle exceptions thrown during its execution.
Callable is an improvement over Runnable that lets functions return values and throw checked exceptions, providing greater flexibility in concurrent programming scenarios
Also, Read: How to achieve thread-safety in Java?