|
Answer» Both the interfaces are generally used to encapsulate tasks that are needed to be executed by another THREAD. But there are some DIFFERENCES between them as given below:
Running Interface: This interface is basically available in Java right from the beginning. It is simply used to execute code on a concurrent thread. Callable Interface: This interface is basically a new one that was introduced as a part of the concurrency package. It addresses the limitation of RUNNABLE interfaces along with some major changes like generics, enum, static imports, variable argument method, etc. It uses generics to define the RETURN type of object. public interface Runnable { public abstract void run(); } public interface Callable<V> { V call() throws Exception; } Runnable Interface vs Callable Interface | Runnable Interface | Callable Interface |
|---|
| It does not return any result and therefore, cannot throw a checked exception. | It returns a result and therefore, can throw an exception. | | It cannot be passed to invokeAll method. | It can be passed to invokeAll method. | | It was introduced in JDK 1.0. | It was introduced in JDK 5.0, so one cannot USE it before Java 5. | | It simply belongs to Java.lang. | It simply belongs to java.util.concurrent. | | It uses the run() method to define a task. | It uses the call() method to define a task. | | To use this interface, one needs to override the run() method. | To use this interface, one needs to override the call() method. |
|