Explain the difference between runnable and callable interface in java ?
How runnable interface is different from callable interface in java ?: Both runnable interface and callable interface in java are typically used to encapsulating the tasks that must be carried out by a different thread. The article provides the major difference between the runnable interface and callable interface in java
Runnable Interface in java :
- Runnable is a functional interface which is used to create a thread at runtime
- Runnable interface provides only abstract method run()
- When using runnable interface, multiple threads share the same objects
- When using runnable interface, memory consumption is less
- When using runnable interface , a class can extend another class
- Runnable interface is introduced in JDK 10
public class PlanforexamsLogger implements Runnable{
private Logger logger
= LoggerFactory.getLogger(PlanforexamsLogger.class);
@Override
public void run() {
logger.info("Messge from PlanforexamsLogger Class ...");
}
}Callable Interface in java :
- Callable interface is part of the concurrent package in java
- Callable interface is introduced in JDK 5.0
- Callable interface provides the call() method to define a task
- Callable interface provides the result and thus throws an exception for the error occurred
Callable Interface Example in java
Difference between the runnable interface and callable interface in java
| Runnable Interface in java | Callable Interface in java |
|---|---|
| Runnable Interface in java does not Result and thus do not support throwing an exception. | Callable Interface in java returns Result and thus allows throwing an exception |
| Runnable Interface in java cannot be passed to invokeAll() method. | Callable Interface in java can be passed to invokeAll() method. |
| Runnable Interface in java was introduced in JDK 1.0. | Callable Interface in java was introduced in JDK 5.0 |
| Runnable Interface class is in the package Java.lang | Callable Interface class is in the package java.util.concurrent. |
| Runnable Interface in java provides the run() method to define a task. | Callable Interface in java provides the call() method to define a task. |
| Runnable Interface in java allows to override the run() method | Callable Interface in java allows to override the call() method. |

Leave a Reply