☕ Java Advanced

What is the difference between Callable and Runnable in Java?

Answer

Both Runnable and Callable represent tasks to be executed by a thread or executor, but they differ in two ways. Runnable has a run() method with no return value (void) and cannot throw checked exceptions. Callable<V> has a call() method that returns a value of type V and can throw checked exceptions. Callable is submitted to an ExecutorService via submit(), which returns a Future<V> that can be used to retrieve the result or check for exceptions. Runnable can be used with both Thread and ExecutorService. For tasks that need to return results or propagate checked exceptions, Callable with Future (or CompletableFuture) is the right choice.