Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/util/concurrent/Future.java
+++ new/src/share/classes/java/util/concurrent/Future.java
1 1 /*
2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 3 *
4 4 * This code is free software; you can redistribute it and/or modify it
5 5 * under the terms of the GNU General Public License version 2 only, as
6 6 * published by the Free Software Foundation. Oracle designates this
7 7 * particular file as subject to the "Classpath" exception as provided
8 8 * by Oracle in the LICENSE file that accompanied this code.
9 9 *
10 10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 13 * version 2 for more details (a copy is included in the LICENSE file that
14 14 * accompanied this code).
15 15 *
16 16 * You should have received a copy of the GNU General Public License version
17 17 * 2 along with this work; if not, write to the Free Software Foundation,
18 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 19 *
20 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 21 * or visit www.oracle.com if you need additional information or have any
22 22 * questions.
23 23 */
24 24
25 25 /*
26 26 * This file is available under and governed by the GNU General Public
27 27 * License version 2 only, as published by the Free Software Foundation.
28 28 * However, the following notice accompanied the original version of this
29 29 * file:
30 30 *
31 31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 32 * Expert Group and released to the public domain, as explained at
33 33 * http://creativecommons.org/licenses/publicdomain
34 34 */
35 35
36 36 package java.util.concurrent;
37 37
38 38 /**
39 39 * A <tt>Future</tt> represents the result of an asynchronous
↓ open down ↓ |
39 lines elided |
↑ open up ↑ |
40 40 * computation. Methods are provided to check if the computation is
41 41 * complete, to wait for its completion, and to retrieve the result of
42 42 * the computation. The result can only be retrieved using method
43 43 * <tt>get</tt> when the computation has completed, blocking if
44 44 * necessary until it is ready. Cancellation is performed by the
45 45 * <tt>cancel</tt> method. Additional methods are provided to
46 46 * determine if the task completed normally or was cancelled. Once a
47 47 * computation has completed, the computation cannot be cancelled.
48 48 * If you would like to use a <tt>Future</tt> for the sake
49 49 * of cancellability but not provide a usable result, you can
50 - * declare types of the form <tt>Future<?></tt> and
50 + * declare types of the form {@code Future<?>} and
51 51 * return <tt>null</tt> as a result of the underlying task.
52 52 *
53 53 * <p>
54 54 * <b>Sample Usage</b> (Note that the following classes are all
55 55 * made-up.) <p>
56 - * <pre>
56 + * <pre> {@code
57 57 * interface ArchiveSearcher { String search(String target); }
58 58 * class App {
59 59 * ExecutorService executor = ...
60 60 * ArchiveSearcher searcher = ...
61 61 * void showSearch(final String target)
62 62 * throws InterruptedException {
63 - * Future<String> future
64 - * = executor.submit(new Callable<String>() {
63 + * Future<String> future
64 + * = executor.submit(new Callable<String>() {
65 65 * public String call() {
66 66 * return searcher.search(target);
67 67 * }});
68 68 * displayOtherThings(); // do other things while searching
69 69 * try {
70 70 * displayText(future.get()); // use future
71 71 * } catch (ExecutionException ex) { cleanup(); return; }
72 72 * }
73 - * }
74 - * </pre>
73 + * }}</pre>
75 74 *
76 75 * The {@link FutureTask} class is an implementation of <tt>Future</tt> that
77 76 * implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>.
78 77 * For example, the above construction with <tt>submit</tt> could be replaced by:
79 - * <pre>
80 - * FutureTask<String> future =
81 - * new FutureTask<String>(new Callable<String>() {
78 + * <pre> {@code
79 + * FutureTask<String> future =
80 + * new FutureTask<String>(new Callable<String>() {
82 81 * public String call() {
83 82 * return searcher.search(target);
84 83 * }});
85 - * executor.execute(future);
86 - * </pre>
84 + * executor.execute(future);}</pre>
87 85 *
88 86 * <p>Memory consistency effects: Actions taken by the asynchronous computation
89 87 * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
90 88 * actions following the corresponding {@code Future.get()} in another thread.
91 89 *
92 90 * @see FutureTask
93 91 * @see Executor
94 92 * @since 1.5
95 93 * @author Doug Lea
96 94 * @param <V> The result type returned by this Future's <tt>get</tt> method
97 95 */
98 96 public interface Future<V> {
99 97
100 98 /**
101 99 * Attempts to cancel execution of this task. This attempt will
102 100 * fail if the task has already completed, has already been cancelled,
103 101 * or could not be cancelled for some other reason. If successful,
104 102 * and this task has not started when <tt>cancel</tt> is called,
105 103 * this task should never run. If the task has already started,
106 104 * then the <tt>mayInterruptIfRunning</tt> parameter determines
107 105 * whether the thread executing this task should be interrupted in
108 106 * an attempt to stop the task.
109 107 *
110 108 * <p>After this method returns, subsequent calls to {@link #isDone} will
111 109 * always return <tt>true</tt>. Subsequent calls to {@link #isCancelled}
112 110 * will always return <tt>true</tt> if this method returned <tt>true</tt>.
113 111 *
114 112 * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
115 113 * task should be interrupted; otherwise, in-progress tasks are allowed
116 114 * to complete
117 115 * @return <tt>false</tt> if the task could not be cancelled,
118 116 * typically because it has already completed normally;
119 117 * <tt>true</tt> otherwise
120 118 */
121 119 boolean cancel(boolean mayInterruptIfRunning);
122 120
123 121 /**
124 122 * Returns <tt>true</tt> if this task was cancelled before it completed
125 123 * normally.
126 124 *
127 125 * @return <tt>true</tt> if this task was cancelled before it completed
128 126 */
129 127 boolean isCancelled();
130 128
131 129 /**
132 130 * Returns <tt>true</tt> if this task completed.
133 131 *
134 132 * Completion may be due to normal termination, an exception, or
135 133 * cancellation -- in all of these cases, this method will return
136 134 * <tt>true</tt>.
137 135 *
138 136 * @return <tt>true</tt> if this task completed
139 137 */
140 138 boolean isDone();
141 139
142 140 /**
143 141 * Waits if necessary for the computation to complete, and then
144 142 * retrieves its result.
145 143 *
146 144 * @return the computed result
147 145 * @throws CancellationException if the computation was cancelled
148 146 * @throws ExecutionException if the computation threw an
149 147 * exception
150 148 * @throws InterruptedException if the current thread was interrupted
151 149 * while waiting
152 150 */
153 151 V get() throws InterruptedException, ExecutionException;
154 152
155 153 /**
156 154 * Waits if necessary for at most the given time for the computation
157 155 * to complete, and then retrieves its result, if available.
158 156 *
159 157 * @param timeout the maximum time to wait
160 158 * @param unit the time unit of the timeout argument
161 159 * @return the computed result
162 160 * @throws CancellationException if the computation was cancelled
163 161 * @throws ExecutionException if the computation threw an
164 162 * exception
165 163 * @throws InterruptedException if the current thread was interrupted
166 164 * while waiting
167 165 * @throws TimeoutException if the wait timed out
168 166 */
169 167 V get(long timeout, TimeUnit unit)
170 168 throws InterruptedException, ExecutionException, TimeoutException;
171 169 }
↓ open down ↓ |
75 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX