ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Future.java
Revision: 1.36
Committed: Wed May 9 18:56:54 2012 UTC (12 years ago) by jsr166
Branch: MAIN
Changes since 1.35: +6 -6 lines
Log Message:
code snippet whitespace

File Contents

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