java.util.concurrent
Interface Future<V>

All Superinterfaces:
Cancellable
All Known Subinterfaces:
ScheduledFuture
All Known Implementing Classes:
FutureTask, CancellableTask.InnerCancellableFuture

public interface Future<V>
extends Cancellable

A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Once the computation has completed, the computation cannot be cancelled.

Sample Usage (Note that the following classes are all made-up.)

 interface ArchiveSearcher { String search(String target); }
 class App {
   Executor executor = ...
   ArchiveSearcher searcher = ...
   void showSearch(final String target) throws InterruptedException {
     Future<String> future =
       new FutureTask<String>(new Callable<String>() {
         public String call() {
           return searcher.search(target);
       }});
     executor.execute(future);
     displayOtherThings(); // do other things while searching
     try {
       displayText(future.get()); // use future
     } catch (ExecutionException ex) { cleanup(); return; }
   }
 }
 
The Executors class contains more convenient methods for common usages. For example, the above explicit construction could be replaced with:
 Future<String> future = Executors.execute(executor, 
    new Callable<String>() {
       public String call() {
         return searcher.search(target);
    }});
 

Since:
1.5
Author:
Doug Lea
See Also:
FutureTask, Executor

Method Summary
 V get()
          Waits if necessary for computation to complete, and then retrieves its result.
 V get(long timeout, TimeUnit granularity)
          Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.
 
Methods inherited from interface java.util.concurrent.Cancellable
cancel, isCancelled, isDone
 

Method Detail

get

public V get()
      throws java.lang.InterruptedException,
             ExecutionException
Waits if necessary for computation to complete, and then retrieves its result.

Returns:
computed result
Throws:
CancellationException - if this future was cancelled.
ExecutionException - if underlying computation threw an exception
java.lang.InterruptedException - if current thread was interrupted while waiting

get

public V get(long timeout,
             TimeUnit granularity)
      throws java.lang.InterruptedException,
             ExecutionException,
             TimeoutException
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result.

Parameters:
timeout - the maximum time to wait
granularity - the time unit of the timeout argument
Returns:
computed result
Throws:
ExecutionException - if underlying computation threw an exception
java.lang.InterruptedException - if current thread was interrupted while waiting
TimeoutException - if the wait timed out