Future interface defined in java 5 represents the result of an asynchronous computation. Future has been provide with some really powerful methods. For Instance -
1) We can check whether a task has been completed or not.
2) We can cancel a task.
3) Check whether task was cancelled or complete normally.
The Future Interface
Future interface looks like this :
Understanding the methods
boolean cancel(boolean mayInterruptIfRunning) -
This method does following -
1) If the process(Thread) has not started, then cancel the thread.
2) If the process has started, then check if
mayInterruptIfRunning = true ==> Inturrupt the thread and cancel it
mayInterruptIfRunning = false ==> Let it run
This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason.
After this method returns, subsequent calls to isDone() will always return true.
Subsequent calls to isCancelled() will always return true if this method returned true.
boolean isCancelled()
Returns true if this task was cancelled before it completed normally
V get() throws InterruptedException, ExecutionException
This method is a blocking call. It will cause JVM to wait if necessary for the computation to complete, and then retrieves its result.
boolean isDone()
Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation - in all of these cases, this method will return true.
Here’s where the combined power of the thread pools and Callable come together. When you submit a Callable to one of the thread pools, you are provided an instance of Future that is typed to the Callable you passed in. This object substitutes for an actual Thread instance that you would have used prior to 1.5. Whereas you previously had to do Thread.join() or Thread.join(long millis), now you may use them as in this example.
Example
CallableImpl.java - Implementing the Callable interface
CallableDemo.java - Understanding the use of future via demo
1) We can check whether a task has been completed or not.
2) We can cancel a task.
3) Check whether task was cancelled or complete normally.
The Future Interface
Future interface looks like this :
public interface Future { //Attempts to cancel execution of this task. boolean cancel(boolean mayInterruptIfRunning); boolean isCancelled(); boolean isDone(); // Waits if necessary for the computation to complete, // and then retrieves its result. V get() throws InterruptedException, ExecutionException; // Waits if necessary for at most the given time for the computation // to complete, and then retrieves its result, if available. V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
Understanding the methods
boolean cancel(boolean mayInterruptIfRunning) -
This method does following -
1) If the process(Thread) has not started, then cancel the thread.
2) If the process has started, then check if
mayInterruptIfRunning = true ==> Inturrupt the thread and cancel it
mayInterruptIfRunning = false ==> Let it run
This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason.
After this method returns, subsequent calls to isDone() will always return true.
Subsequent calls to isCancelled() will always return true if this method returned true.
boolean isCancelled()
Returns true if this task was cancelled before it completed normally
V get() throws InterruptedException, ExecutionException
This method is a blocking call. It will cause JVM to wait if necessary for the computation to complete, and then retrieves its result.
boolean isDone()
Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation - in all of these cases, this method will return true.
Here’s where the combined power of the thread pools and Callable come together. When you submit a Callable to one of the thread pools, you are provided an instance of Future that is typed to the Callable you passed in. This object substitutes for an actual Thread instance that you would have used prior to 1.5. Whereas you previously had to do Thread.join() or Thread.join(long millis), now you may use them as in this example.
Example
CallableImpl.java - Implementing the Callable interface
package com.vaani.callable; import java.util.concurrent.Callable; public class CallableImpl implements Callable<Integer> { private int name; public CallableImpl(int i){ name = i; } public Integer call() { for(int i = 0; i < 10; i++) { System.out.println("Thread : " + getName() + " I is : " + i); } return new Integer(getName()); } public int getName() { return name; } public void setMyName(int myName) { this.name = myName; } }
CallableDemo.java - Understanding the use of future via demo
public class CallableDemo { public static void main(String[] args) { Callable<Integer> callable = new CallableImpl(2); ExecutorService executor = new ScheduledThreadPoolExecutor(5); Future<Integer> future = executor.submit(callable); try { System.out.println("Future value: " + future.get()); } catch (Exception e) { e.printStackTrace(); } } }
No comments:
Post a Comment