What Callable and Runnable Interfaces in java
The call() method is called in order to execute the asynchronous task. The 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.
The call() method can also thrown an Exception in case the task fails during execution.
Java Feature
==========
A Java Future, java.util.concurrent.Future, represents the result of an asynchronous computation. When the asynchronous task is created, a Java Future object is returned. This Future object functions as a handle to the result of the asynchronous task. Once the asynchronous task completes, the result can be accessed via the Future object returned when the task was started.
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning)
V get();
V get(long timeout, TimeUnit unit);
boolean isCancelled();
boolean isDone();
}
Runnable
|
Callable
|
Runnable cannot
be parameterized .
Runnable has
run() method .
Runnable.run()
returns void .
Can not throw
Checked Exceptions .
|
Callable is a parameterised type whose type parameter indicates the return type of its run
method |
Callable has
call() method
Callable.call()
returns a value of Type T
Can throw Checked
Exceptions
|
Reviewed by Mukesh Jha
on
12:05 AM
Rating:


No comments:
Add your comment