35 |
|
private final Sync sync; |
36 |
|
|
37 |
|
/** |
38 |
< |
* Creates a <tt>FutureTask</tt> that will upon running, execute the |
38 |
> |
* Creates a <tt>FutureTask</tt> that will, upon running, execute the |
39 |
|
* given <tt>Callable</tt>. |
40 |
|
* |
41 |
|
* @param callable the callable task |
48 |
|
} |
49 |
|
|
50 |
|
/** |
51 |
< |
* Creates a <tt>FutureTask</tt> that will upon running, execute the |
51 |
> |
* Creates a <tt>FutureTask</tt> that will, upon running, execute the |
52 |
|
* 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 |
55 |
> |
* @param runnable the runnable task |
56 |
|
* @param result the result to return on successful completion. If |
57 |
|
* you don't need a particular result, consider using |
58 |
|
* constructions of the form: |
160 |
|
private final class Sync extends AbstractQueuedSynchronizer { |
161 |
|
private static final long serialVersionUID = -7828117401763700385L; |
162 |
|
|
163 |
+ |
/** State value representing that task is ready to run */ |
164 |
+ |
private static final int READY = 0; |
165 |
|
/** State value representing that task is running */ |
166 |
|
private static final int RUNNING = 1; |
167 |
|
/** State value representing that task ran */ |
195 |
|
* Implements AQS base acquire to succeed if ran or cancelled |
196 |
|
*/ |
197 |
|
protected int tryAcquireShared(int ignore) { |
198 |
< |
return innerIsDone()? 1 : -1; |
198 |
> |
return innerIsDone() ? 1 : -1; |
199 |
|
} |
200 |
|
|
201 |
|
/** |
217 |
|
|
218 |
|
V innerGet() throws InterruptedException, ExecutionException { |
219 |
|
acquireSharedInterruptibly(0); |
220 |
< |
if (getState() == CANCELLED) |
221 |
< |
throw new CancellationException(); |
222 |
< |
if (exception != null) |
223 |
< |
throw new ExecutionException(exception); |
224 |
< |
return result; |
220 |
> |
if (getState() == CANCELLED) |
221 |
> |
throw new CancellationException(); |
222 |
> |
if (exception != null) |
223 |
> |
throw new ExecutionException(exception); |
224 |
> |
return result; |
225 |
|
} |
226 |
|
|
227 |
|
V innerGet(long nanosTimeout) throws InterruptedException, ExecutionException, TimeoutException { |
269 |
|
} |
270 |
|
if (compareAndSetState(s, RAN)) { |
271 |
|
exception = t; |
270 |
– |
result = null; |
272 |
|
releaseShared(0); |
273 |
|
done(); |
274 |
|
return; |
295 |
|
} |
296 |
|
|
297 |
|
void innerRun() { |
298 |
< |
if (!compareAndSetState(0, RUNNING)) |
298 |
> |
if (!compareAndSetState(READY, RUNNING)) |
299 |
|
return; |
300 |
|
try { |
301 |
|
runner = Thread.currentThread(); |
302 |
|
if (getState() == RUNNING) // recheck after setting thread |
303 |
< |
innerSet(callable.call()); |
303 |
> |
set(callable.call()); |
304 |
|
else |
305 |
|
releaseShared(0); // cancel |
306 |
|
} catch (Throwable ex) { |
307 |
< |
innerSetException(ex); |
307 |
> |
setException(ex); |
308 |
|
} |
309 |
|
} |
310 |
|
|
311 |
|
boolean innerRunAndReset() { |
312 |
< |
if (!compareAndSetState(0, RUNNING)) |
312 |
> |
if (!compareAndSetState(READY, RUNNING)) |
313 |
|
return false; |
314 |
|
try { |
315 |
|
runner = Thread.currentThread(); |
316 |
|
if (getState() == RUNNING) |
317 |
|
callable.call(); // don't set result |
318 |
|
runner = null; |
319 |
< |
return compareAndSetState(RUNNING, 0); |
319 |
> |
return compareAndSetState(RUNNING, READY); |
320 |
|
} catch (Throwable ex) { |
321 |
< |
innerSetException(ex); |
321 |
> |
setException(ex); |
322 |
|
return false; |
323 |
|
} |
324 |
|
} |