1   /*
2    * %W% %E%
3    *
4    * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5    * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6    */
7   
8   package java.util.concurrent;
9   
10  /**
11   * A <tt>Future</tt> represents the result of an asynchronous
12   * computation.  Methods are provided to check if the computation is
13   * complete, to wait for its completion, and to retrieve the result of
14   * the computation.  The result can only be retrieved using method
15   * <tt>get</tt> when the computation has completed, blocking if
16   * necessary until it is ready.  Cancellation is performed by the
17   * <tt>cancel</tt> method.  Additional methods are provided to
18   * determine if the task completed normally or was cancelled. Once a
19   * computation has completed, the computation cannot be cancelled.
20   * If you would like to use a <tt>Future</tt> for the sake
21   * of cancellability but not provide a usable result, you can
22   * declare types of the form <tt>Future&lt;?&gt;</tt> and
23   * return <tt>null</tt> as a result of the underlying task.
24   *
25   * <p>
26   * <b>Sample Usage</b> (Note that the following classes are all
27   * made-up.) <p>
28   * <pre>
29   * interface ArchiveSearcher { String search(String target); }
30   * class App {
31   *   ExecutorService executor = ...
32   *   ArchiveSearcher searcher = ...
33   *   void showSearch(final String target)
34   *       throws InterruptedException {
35   *     Future&lt;String&gt; future
36   *       = executor.submit(new Callable&lt;String&gt;() {
37   *         public String call() {
38   *             return searcher.search(target);
39   *         }});
40   *     displayOtherThings(); // do other things while searching
41   *     try {
42   *       displayText(future.get()); // use future
43   *     } catch (ExecutionException ex) { cleanup(); return; }
44   *   }
45   * }
46   * </pre>
47   *
48   * The {@link FutureTask} class is an implementation of <tt>Future</tt> that
49   * implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>.
50   * For example, the above construction with <tt>submit</tt> could be replaced by:
51   * <pre>
52   *     FutureTask&lt;String&gt; future =
53   *       new FutureTask&lt;String&gt;(new Callable&lt;String&gt;() {
54   *         public String call() {
55   *           return searcher.search(target);
56   *       }});
57   *     executor.execute(future);
58   * </pre>
59   *
60   * <p>Memory consistency effects: Actions taken by the asynchronous computation
61   * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
62   * actions following the corresponding {@code Future.get()} in another thread.
63   *
64   * @see FutureTask
65   * @see Executor
66   * @since 1.5
67   * @author Doug Lea
68   * @param <V> The result type returned by this Future's <tt>get</tt> method
69   */
70  public interface Future<V> {
71  
72      /**
73       * Attempts to cancel execution of this task.  This attempt will
74       * fail if the task has already completed, has already been cancelled,
75       * or could not be cancelled for some other reason. If successful,
76       * and this task has not started when <tt>cancel</tt> is called,
77       * this task should never run.  If the task has already started,
78       * then the <tt>mayInterruptIfRunning</tt> parameter determines
79       * whether the thread executing this task should be interrupted in
80       * an attempt to stop the task.
81       *
82       * <p>After this method returns, subsequent calls to {@link #isDone} will
83       * always return <tt>true</tt>.  Subsequent calls to {@link #isCancelled}
84       * will always return <tt>true</tt> if this method returned <tt>true</tt>.
85       *
86       * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
87       * task should be interrupted; otherwise, in-progress tasks are allowed
88       * to complete
89       * @return <tt>false</tt> if the task could not be cancelled,
90       * typically because it has already completed normally;
91       * <tt>true</tt> otherwise
92       */
93      boolean cancel(boolean mayInterruptIfRunning);
94  
95      /**
96       * Returns <tt>true</tt> if this task was cancelled before it completed
97       * normally.
98       *
99       * @return <tt>true</tt> if this task was cancelled before it completed
100      */
101     boolean isCancelled();
102 
103     /**
104      * Returns <tt>true</tt> if this task completed.
105      *
106      * Completion may be due to normal termination, an exception, or
107      * cancellation -- in all of these cases, this method will return
108      * <tt>true</tt>.
109      *
110      * @return <tt>true</tt> if this task completed
111      */
112     boolean isDone();
113 
114     /**
115      * Waits if necessary for the computation to complete, and then
116      * retrieves its result.
117      *
118      * @return the computed result
119      * @throws CancellationException if the computation was cancelled
120      * @throws ExecutionException if the computation threw an
121      * exception
122      * @throws InterruptedException if the current thread was interrupted
123      * while waiting
124      */
125     V get() throws InterruptedException, ExecutionException;
126 
127     /**
128      * Waits if necessary for at most the given time for the computation
129      * to complete, and then retrieves its result, if available.
130      *
131      * @param timeout the maximum time to wait
132      * @param unit the time unit of the timeout argument
133      * @return the computed result
134      * @throws CancellationException if the computation was cancelled
135      * @throws ExecutionException if the computation threw an
136      * exception
137      * @throws InterruptedException if the current thread was interrupted
138      * while waiting
139      * @throws TimeoutException if the wait timed out
140      */
141     V get(long timeout, TimeUnit unit)
142         throws InterruptedException, ExecutionException, TimeoutException;
143 }
144