Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
What is the difference between Runnable and Callable?
| Runnable | Callable<V> |
Method: void run() | Method: V call() throws Exception |
| Cannot return a result. | Returns a value of type V. |
| Cannot throw checked exceptions. | Can throw checked exceptions. |
| Usable with plain Thread or an Executor. | Only usable with an ExecutorService, via submit(). |
When you submit a Runnable to an ExecutorService, you get back a Future<?> whose result is always null. Submitting a Callable<V> gives a Future<V> that will hold the actual computed value, or the exception if call() threw one.
Choose Runnable for fire-and-forget work with no result, and Callable when you need a value back or need to propagate a checked exception from the task.
More Related questions...