57 |
|
* will be null. |
58 |
|
*/ |
59 |
|
|
60 |
< |
/** The runnable, if non-null, then callable is null */ |
61 |
< |
final Runnable runnable; |
62 |
< |
/** The callable, if non-null, then runnable is null */ |
63 |
< |
final Callable<V> callable; |
60 |
> |
/** The runnable; if non-null, then callable is null */ |
61 |
> |
private final Runnable runnable; |
62 |
> |
/** The callable; if non-null, then runnable is null */ |
63 |
> |
private final Callable<V> callable; |
64 |
|
|
65 |
|
/** The result to return from get() */ |
66 |
|
private V result; |
95 |
|
* <tt>Boolean.TRUE</tt>. |
96 |
|
* @throws NullPointerException if runnable is null |
97 |
|
*/ |
98 |
< |
public FutureTask(final Runnable runnable, final V result) { |
98 |
> |
public FutureTask(Runnable runnable, V result) { |
99 |
|
if (runnable == null) |
100 |
|
throw new NullPointerException(); |
101 |
|
this.runnable = runnable; |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
+ |
* Sets this Future to the results of computation and then resets |
301 |
+ |
* to initial state. This may be useful for tasks that must |
302 |
+ |
* execute more than once. |
303 |
+ |
*/ |
304 |
+ |
protected void runAndReset() { |
305 |
+ |
if (setRunning()) { |
306 |
+ |
try { |
307 |
+ |
try { |
308 |
+ |
if (runnable != null) |
309 |
+ |
runnable.run(); |
310 |
+ |
else if (callable != null) |
311 |
+ |
set(callable.call()); |
312 |
+ |
} catch(Throwable ex) { |
313 |
+ |
setException(ex); |
314 |
+ |
} |
315 |
+ |
} finally { |
316 |
+ |
reset(); |
317 |
+ |
} |
318 |
+ |
} |
319 |
+ |
} |
320 |
+ |
|
321 |
+ |
/** |
322 |
|
* Protected method invoked when this task transitions to state |
323 |
|
* <tt>isDone</tt> (whether normally or via cancellation). The |
324 |
|
* default implementation does nothing. Subclasses may override |
347 |
|
lock.unlock(); |
348 |
|
} |
349 |
|
} |
328 |
– |
} |
329 |
– |
|
330 |
– |
|
331 |
– |
|
332 |
– |
|
333 |
– |
|
334 |
– |
|
335 |
– |
|
350 |
|
|
351 |
+ |
/** |
352 |
+ |
* Return the <tt>Runnable</tt> or <tt>Callable</tt> used |
353 |
+ |
* in the constructor for this <tt>Future</tt>. |
354 |
+ |
* @return the task |
355 |
+ |
*/ |
356 |
+ |
protected Object getTask() { |
357 |
+ |
if (runnable != null) |
358 |
+ |
return runnable; |
359 |
+ |
else |
360 |
+ |
return callable; |
361 |
+ |
} |
362 |
|
|
363 |
+ |
} |
364 |
|
|