1 |
tim |
1.1 |
/* |
2 |
dl |
1.2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.23 |
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
tim |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
dl |
1.13 |
import java.util.concurrent.locks.*; |
9 |
|
|
|
10 |
tim |
1.1 |
/** |
11 |
dl |
1.8 |
* A cancellable asynchronous computation. This class provides a base |
12 |
|
|
* implementation of {@link Future}, with methods to start and cancel |
13 |
|
|
* a computation, query to see if the computation is complete, and |
14 |
dl |
1.4 |
* retrieve the result of the computation. The result can only be |
15 |
|
|
* retrieved when the computation has completed; the <tt>get</tt> |
16 |
|
|
* method will block if the computation has not yet completed. Once |
17 |
dl |
1.8 |
* the computation has completed, the computation cannot be restarted |
18 |
|
|
* or cancelled. |
19 |
tim |
1.1 |
* |
20 |
dl |
1.8 |
* <p>A <tt>FutureTask</tt> can be used to wrap a {@link Callable} or |
21 |
|
|
* {@link java.lang.Runnable} object. Because <tt>FutureTask</tt> |
22 |
|
|
* implements <tt>Runnable</tt>, a <tt>FutureTask</tt> can be |
23 |
|
|
* submitted to an {@link Executor} for execution. |
24 |
tim |
1.1 |
* |
25 |
dl |
1.14 |
* <p>In addition to serving as a standalone class, this class provides |
26 |
|
|
* <tt>protected</tt> functionality that may be useful when creating |
27 |
|
|
* customized task classes. |
28 |
|
|
* |
29 |
tim |
1.1 |
* @since 1.5 |
30 |
dl |
1.4 |
* @author Doug Lea |
31 |
dl |
1.12 |
* @param <V> The result type returned by this FutureTask's <tt>get</tt> method |
32 |
tim |
1.1 |
*/ |
33 |
peierls |
1.39 |
public class FutureTask<V> implements RunnableFuture<V> { |
34 |
dl |
1.24 |
/** Synchronization control for FutureTask */ |
35 |
|
|
private final Sync sync; |
36 |
dl |
1.11 |
|
37 |
tim |
1.1 |
/** |
38 |
dl |
1.31 |
* Creates a <tt>FutureTask</tt> that will upon running, execute the |
39 |
tim |
1.1 |
* given <tt>Callable</tt>. |
40 |
|
|
* |
41 |
|
|
* @param callable the callable task |
42 |
dl |
1.9 |
* @throws NullPointerException if callable is null |
43 |
tim |
1.1 |
*/ |
44 |
|
|
public FutureTask(Callable<V> callable) { |
45 |
dl |
1.9 |
if (callable == null) |
46 |
|
|
throw new NullPointerException(); |
47 |
dl |
1.24 |
sync = new Sync(callable); |
48 |
tim |
1.1 |
} |
49 |
|
|
|
50 |
|
|
/** |
51 |
dl |
1.31 |
* Creates a <tt>FutureTask</tt> that will upon running, execute the |
52 |
tim |
1.1 |
* given <tt>Runnable</tt>, and arrange that <tt>get</tt> will return the |
53 |
|
|
* given result on successful completion. |
54 |
|
|
* |
55 |
|
|
* @param runnable the runnable task |
56 |
|
|
* @param result the result to return on successful completion. If |
57 |
dl |
1.9 |
* you don't need a particular result, consider using |
58 |
dl |
1.16 |
* constructions of the form: |
59 |
|
|
* <tt>Future<?> f = new FutureTask<Object>(runnable, null)</tt> |
60 |
dl |
1.9 |
* @throws NullPointerException if runnable is null |
61 |
tim |
1.1 |
*/ |
62 |
dl |
1.15 |
public FutureTask(Runnable runnable, V result) { |
63 |
dl |
1.24 |
sync = new Sync(Executors.callable(runnable, result)); |
64 |
dl |
1.20 |
} |
65 |
|
|
|
66 |
|
|
public boolean isCancelled() { |
67 |
dl |
1.27 |
return sync.innerIsCancelled(); |
68 |
dl |
1.20 |
} |
69 |
jsr166 |
1.35 |
|
70 |
dl |
1.20 |
public boolean isDone() { |
71 |
dl |
1.27 |
return sync.innerIsDone(); |
72 |
dl |
1.13 |
} |
73 |
|
|
|
74 |
|
|
public boolean cancel(boolean mayInterruptIfRunning) { |
75 |
dl |
1.27 |
return sync.innerCancel(mayInterruptIfRunning); |
76 |
dl |
1.13 |
} |
77 |
jsr166 |
1.35 |
|
78 |
jsr166 |
1.43 |
/** |
79 |
|
|
* @throws CancellationException {@inheritDoc} |
80 |
|
|
*/ |
81 |
dl |
1.2 |
public V get() throws InterruptedException, ExecutionException { |
82 |
dl |
1.27 |
return sync.innerGet(); |
83 |
tim |
1.1 |
} |
84 |
|
|
|
85 |
jsr166 |
1.43 |
/** |
86 |
|
|
* @throws CancellationException {@inheritDoc} |
87 |
|
|
*/ |
88 |
dl |
1.2 |
public V get(long timeout, TimeUnit unit) |
89 |
tim |
1.1 |
throws InterruptedException, ExecutionException, TimeoutException { |
90 |
dl |
1.27 |
return sync.innerGet(unit.toNanos(timeout)); |
91 |
tim |
1.1 |
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
dl |
1.20 |
* Protected method invoked when this task transitions to state |
95 |
|
|
* <tt>isDone</tt> (whether normally or via cancellation). The |
96 |
|
|
* default implementation does nothing. Subclasses may override |
97 |
|
|
* this method to invoke completion callbacks or perform |
98 |
|
|
* bookkeeping. Note that you can query status inside the |
99 |
|
|
* implementation of this method to determine whether this task |
100 |
|
|
* has been cancelled. |
101 |
|
|
*/ |
102 |
|
|
protected void done() { } |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* Sets the result of this Future to the given value unless |
106 |
dl |
1.29 |
* this future has already been set or has been cancelled. |
107 |
dl |
1.40 |
* This method is invoked internally by the <tt>run</tt> method |
108 |
|
|
* upon successful completion of the computation. |
109 |
tim |
1.1 |
* @param v the value |
110 |
jsr166 |
1.35 |
*/ |
111 |
dl |
1.2 |
protected void set(V v) { |
112 |
dl |
1.27 |
sync.innerSet(v); |
113 |
tim |
1.1 |
} |
114 |
|
|
|
115 |
|
|
/** |
116 |
dl |
1.13 |
* Causes this future to report an <tt>ExecutionException</tt> |
117 |
dl |
1.20 |
* with the given throwable as its cause, unless this Future has |
118 |
dl |
1.24 |
* already been set or has been cancelled. |
119 |
dl |
1.40 |
* This method is invoked internally by the <tt>run</tt> method |
120 |
|
|
* upon failure of the computation. |
121 |
jsr166 |
1.41 |
* @param t the cause of failure |
122 |
jsr166 |
1.35 |
*/ |
123 |
dl |
1.2 |
protected void setException(Throwable t) { |
124 |
dl |
1.27 |
sync.innerSetException(t); |
125 |
tim |
1.1 |
} |
126 |
jsr166 |
1.35 |
|
127 |
jsr166 |
1.44 |
// The following (duplicated) doc comment can be removed once |
128 |
|
|
// |
129 |
|
|
// 6270645: Javadoc comments should be inherited from most derived |
130 |
|
|
// superinterface or superclass |
131 |
|
|
// is fixed. |
132 |
|
|
/** |
133 |
|
|
* Sets this Future to the result of its computation |
134 |
|
|
* unless it has been cancelled. |
135 |
|
|
*/ |
136 |
dl |
1.24 |
public void run() { |
137 |
dl |
1.27 |
sync.innerRun(); |
138 |
dl |
1.24 |
} |
139 |
|
|
|
140 |
|
|
/** |
141 |
dl |
1.30 |
* Executes the computation without setting its result, and then |
142 |
|
|
* resets this Future to initial state, failing to do so if the |
143 |
dl |
1.24 |
* computation encounters an exception or is cancelled. This is |
144 |
|
|
* designed for use with tasks that intrinsically execute more |
145 |
|
|
* than once. |
146 |
|
|
* @return true if successfully run and reset |
147 |
|
|
*/ |
148 |
|
|
protected boolean runAndReset() { |
149 |
dl |
1.27 |
return sync.innerRunAndReset(); |
150 |
dl |
1.14 |
} |
151 |
dl |
1.3 |
|
152 |
dl |
1.14 |
/** |
153 |
dl |
1.24 |
* Synchronization control for FutureTask. Note that this must be |
154 |
dl |
1.30 |
* a non-static inner class in order to invoke the protected |
155 |
dl |
1.24 |
* <tt>done</tt> method. For clarity, all inner class support |
156 |
dl |
1.27 |
* methods are same as outer, prefixed with "inner". |
157 |
dl |
1.24 |
* |
158 |
|
|
* Uses AQS sync state to represent run status |
159 |
dl |
1.20 |
*/ |
160 |
dl |
1.24 |
private final class Sync extends AbstractQueuedSynchronizer { |
161 |
dl |
1.42 |
private static final long serialVersionUID = -7828117401763700385L; |
162 |
|
|
|
163 |
dl |
1.24 |
/** State value representing that task is running */ |
164 |
|
|
private static final int RUNNING = 1; |
165 |
|
|
/** State value representing that task ran */ |
166 |
|
|
private static final int RAN = 2; |
167 |
|
|
/** State value representing that task was cancelled */ |
168 |
|
|
private static final int CANCELLED = 4; |
169 |
|
|
|
170 |
|
|
/** The underlying callable */ |
171 |
|
|
private final Callable<V> callable; |
172 |
|
|
/** The result to return from get() */ |
173 |
|
|
private V result; |
174 |
|
|
/** The exception to throw from get() */ |
175 |
|
|
private Throwable exception; |
176 |
|
|
|
177 |
jsr166 |
1.35 |
/** |
178 |
dl |
1.24 |
* The thread running task. When nulled after set/cancel, this |
179 |
|
|
* indicates that the results are accessible. Must be |
180 |
dl |
1.32 |
* volatile, to ensure visibility upon completion. |
181 |
dl |
1.24 |
*/ |
182 |
|
|
private volatile Thread runner; |
183 |
|
|
|
184 |
|
|
Sync(Callable<V> callable) { |
185 |
|
|
this.callable = callable; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
private boolean ranOrCancelled(int state) { |
189 |
|
|
return (state & (RAN | CANCELLED)) != 0; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
/** |
193 |
dl |
1.26 |
* Implements AQS base acquire to succeed if ran or cancelled |
194 |
dl |
1.24 |
*/ |
195 |
dl |
1.26 |
protected int tryAcquireShared(int ignore) { |
196 |
dl |
1.27 |
return innerIsDone()? 1 : -1; |
197 |
dl |
1.24 |
} |
198 |
|
|
|
199 |
|
|
/** |
200 |
|
|
* Implements AQS base release to always signal after setting |
201 |
|
|
* final done status by nulling runner thread. |
202 |
|
|
*/ |
203 |
dl |
1.25 |
protected boolean tryReleaseShared(int ignore) { |
204 |
dl |
1.24 |
runner = null; |
205 |
jsr166 |
1.35 |
return true; |
206 |
dl |
1.24 |
} |
207 |
|
|
|
208 |
dl |
1.27 |
boolean innerIsCancelled() { |
209 |
dl |
1.24 |
return getState() == CANCELLED; |
210 |
|
|
} |
211 |
jsr166 |
1.35 |
|
212 |
dl |
1.27 |
boolean innerIsDone() { |
213 |
dl |
1.24 |
return ranOrCancelled(getState()) && runner == null; |
214 |
|
|
} |
215 |
|
|
|
216 |
dl |
1.27 |
V innerGet() throws InterruptedException, ExecutionException { |
217 |
dl |
1.24 |
acquireSharedInterruptibly(0); |
218 |
|
|
if (getState() == CANCELLED) |
219 |
|
|
throw new CancellationException(); |
220 |
|
|
if (exception != null) |
221 |
|
|
throw new ExecutionException(exception); |
222 |
|
|
return result; |
223 |
|
|
} |
224 |
|
|
|
225 |
dl |
1.27 |
V innerGet(long nanosTimeout) throws InterruptedException, ExecutionException, TimeoutException { |
226 |
dl |
1.28 |
if (!tryAcquireSharedNanos(0, nanosTimeout)) |
227 |
jsr166 |
1.35 |
throw new TimeoutException(); |
228 |
dl |
1.24 |
if (getState() == CANCELLED) |
229 |
|
|
throw new CancellationException(); |
230 |
|
|
if (exception != null) |
231 |
|
|
throw new ExecutionException(exception); |
232 |
|
|
return result; |
233 |
|
|
} |
234 |
|
|
|
235 |
dl |
1.27 |
void innerSet(V v) { |
236 |
jsr166 |
1.38 |
for (;;) { |
237 |
|
|
int s = getState(); |
238 |
|
|
if (ranOrCancelled(s)) |
239 |
|
|
return; |
240 |
|
|
if (compareAndSetState(s, RAN)) |
241 |
|
|
break; |
242 |
|
|
} |
243 |
dl |
1.24 |
result = v; |
244 |
|
|
releaseShared(0); |
245 |
|
|
done(); |
246 |
|
|
} |
247 |
|
|
|
248 |
dl |
1.27 |
void innerSetException(Throwable t) { |
249 |
jsr166 |
1.38 |
for (;;) { |
250 |
|
|
int s = getState(); |
251 |
|
|
if (ranOrCancelled(s)) |
252 |
|
|
return; |
253 |
|
|
if (compareAndSetState(s, RAN)) |
254 |
|
|
break; |
255 |
|
|
} |
256 |
dl |
1.24 |
exception = t; |
257 |
|
|
result = null; |
258 |
|
|
releaseShared(0); |
259 |
|
|
done(); |
260 |
|
|
} |
261 |
|
|
|
262 |
dl |
1.27 |
boolean innerCancel(boolean mayInterruptIfRunning) { |
263 |
jsr166 |
1.38 |
for (;;) { |
264 |
|
|
int s = getState(); |
265 |
|
|
if (ranOrCancelled(s)) |
266 |
|
|
return false; |
267 |
|
|
if (compareAndSetState(s, CANCELLED)) |
268 |
|
|
break; |
269 |
|
|
} |
270 |
dl |
1.24 |
if (mayInterruptIfRunning) { |
271 |
|
|
Thread r = runner; |
272 |
dl |
1.49 |
if (r != null) { |
273 |
|
|
Thread.yield(); // Heuristic to avoid stall on next lines |
274 |
|
|
if (r == runner) |
275 |
|
|
r.interrupt(); |
276 |
|
|
} |
277 |
dl |
1.24 |
} |
278 |
|
|
releaseShared(0); |
279 |
|
|
done(); |
280 |
dl |
1.14 |
return true; |
281 |
|
|
} |
282 |
|
|
|
283 |
dl |
1.27 |
void innerRun() { |
284 |
jsr166 |
1.35 |
if (!compareAndSetState(0, RUNNING)) |
285 |
dl |
1.24 |
return; |
286 |
dl |
1.14 |
try { |
287 |
dl |
1.24 |
runner = Thread.currentThread(); |
288 |
dl |
1.37 |
if (getState() == RUNNING) // recheck after setting thread |
289 |
dl |
1.36 |
innerSet(callable.call()); |
290 |
|
|
else |
291 |
dl |
1.37 |
releaseShared(0); // cancel |
292 |
jsr166 |
1.34 |
} catch (Throwable ex) { |
293 |
dl |
1.27 |
innerSetException(ex); |
294 |
jsr166 |
1.35 |
} |
295 |
dl |
1.14 |
} |
296 |
|
|
|
297 |
dl |
1.27 |
boolean innerRunAndReset() { |
298 |
jsr166 |
1.35 |
if (!compareAndSetState(0, RUNNING)) |
299 |
dl |
1.24 |
return false; |
300 |
dl |
1.15 |
try { |
301 |
dl |
1.24 |
runner = Thread.currentThread(); |
302 |
dl |
1.36 |
if (getState() == RUNNING) |
303 |
|
|
callable.call(); // don't set result |
304 |
dl |
1.24 |
runner = null; |
305 |
|
|
return compareAndSetState(RUNNING, 0); |
306 |
jsr166 |
1.34 |
} catch (Throwable ex) { |
307 |
dl |
1.27 |
innerSetException(ex); |
308 |
dl |
1.24 |
return false; |
309 |
jsr166 |
1.35 |
} |
310 |
dl |
1.14 |
} |
311 |
dl |
1.15 |
} |
312 |
|
|
} |