Java programming language provides two ways to implement a class whose instances are to be executed by a thread.
- By implementing Runnable interface
- By implementing Callable interface
Since there are two options so they must have some differences in the features they offer, that’s what we’ll discuss in this post; differences between Runnable and Callable in Java.
Runnable Vs Callable in Java
1- Part of Java programming language
Runnable interface is there since Java 1.0.Callable interface is added in Java 1.5
2- Interface method
Runnable interface has a single method run() with the method signature as following.
public interface Runnable { public abstract void run(); }
Callable interface has a single method call(), a generic method with the method signature as following.
public interface Callable<V> { V call() throws Exception; }
3- Return value
This is one of major difference between Runnable and Callable in Java. Runnable doesn’t return a result. You can see from the method signature of the run method that the return type is void.
Callable can return a result. Return type of the result is provided at the time of creating Callable instance.
For Example a Callable that returns String.
Callable<String> callableObj = new Callable<String>() { @Override public String call() throws Exception { return "Hello"; } };
4- Exception handling
Since Runnable’s run method doesn’t include a throws clause so it is not possible for a Runnable to throw a checked exception.
Following implementation of Runnable results in compile time error “Exception IOException is not compatible with throws clause in Runnable.run()” as IOException is a checked exception.
Runnable runnable = new Runnable() { @Override public void run() throws IOException{ } };
But it is ok if RunTimeException is thrown.
public void run() throws RuntimeException{ }
Callable’s call method does include a throws clause- V call() throws Exception;
so it is possible for a Callable to throw a checked exception.
Callable<String> callable = new Callable<String>() { @Override public String call() throws InterruptedException { return "Hello"; } };
5- How to execute.
To execute a Runnable task you can use one of the following options-
- Thread class has a constructor where you can pass a Runnable as a parameter.
- You can use execute method of the Executor interface to run a Runnable task.
- You can use submit method of the ExecutorService interface to run a Runnable task.
To execute a Callable task there is no support provided by the Thread class but the concurrent API provides the following options-
- You can use submit method of the ExecutorService interface to run a Callable task.
- ExecutorService also has invokeAll() and invokeAny() methods where Callable is passed as a parameter.
- You can also convert a Runnable object to Callable object by using Executors.callable(Runnable task) provided by utility class Executors.
You may also like
- Java ConcurrentSkipListMap With Examples
- Phaser in Java With Examples
- ConcurrentLinkedDeque in Java With Examples
- Java ScheduledThreadPoolExecutor - Scheduling With ExecutorService
- Static Method Overloading And Overriding in Java
- Convert Instant to Java LocalDateTime, LocalTime
- Speculative Execution in Hadoop Framework
- Setter Dependency Injection in Spring
No comments:
Post a Comment