/* * @(#)Future.java */ package java.util.concurrent; /** * 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 when the computation * has completed. The get method will block until the computation * has completed. Once the computation has completed, the result cannot * be changed, nor can the computation be restarted or cancelled. * *

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

*

 * class ArchiveSearcher { String search(String target); }
 * class App {
 *   Executor executor = ...
 *   ArchiveSearcher searcher = ...
 *   void showSearch(final String target) throws InterruptedException {
 *     Future future =
 *       new FutureTask(new Callable() {
 *         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; }
 *   }
 * }
 * 
* * @since 1.5 * @see FutureTask * @see Executor * * @spec JSR-166 * @revised $Date: 2003/08/05 19:04:37 $ * @editor $Author: dl $ * @author Doug Lea */ public interface Future { /** * Returns true if the underlying task has completed. * * @fixme relation to isDone in Cancellable? * * @return true if underlying task has completed */ boolean isDone(); /** * Waits if necessary for computation to complete, and then * retrieves its result. * * @return computed result * @throws CancellationException here??? * @throws ExecutionException if underlying computation threw an * exception * @throws InterruptedException if current thread was interrupted * while waiting */ V get() throws InterruptedException, ExecutionException; /** * Waits if necessary for at most the given time for the computation * to complete, and then retrieves its result. * * @param timeout the maximum time to wait * @param granularity the time unit of the timeout argument * @return computed result * @throws ExecutionException if underlying computation threw an * exception * @throws InterruptedException if current thread was interrupted * while waiting * @throws TimeoutException if the wait timed out */ V get(long timeout, TimeUnit granularity) throws InterruptedException, ExecutionException, TimeoutException; }