What is the difference between using the Runnable
and Callable
interfaces when designing a concurrent thread in Java, why would you choose one over the other?
Interface Callable<V> Implementors define a single method with no arguments called call. The Callable interface is similar to Runnable , in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.
In the nomenclature of most operating systems, "running" means that the thread actually is executing instructions on some CPU, and "runnable" means that nothing prevents the thread from "running" except the availability of a CPU to run on. A Java program can not tell the difference between those two states.
A runnable interface in Java is an interface whose instances can run as a Thread. While working with Threads, the runnable interface acts as a core element of the Java programming language. Java classes created to run Threads must implement this interface.
Java Callable Interface DefinitionThe call() method can return a result. If the task is executed asynchronously, the result is typically propagated back to the creator of the task via a Java Future. This is the case when a Callable is submitted to an ExecutorService for concurrent execution.
See explanation here.
The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With