21 |
|
* without any real-time guarantees about when, after they are |
22 |
|
* enabled, they will commence. Tasks scheduled for exactly the same |
23 |
|
* execution time are enabled in first-in-first-out (FIFO) order of |
24 |
< |
* submission. Cancelled tasks are automatically removed from the |
25 |
< |
* work queue. |
24 |
> |
* submission. If {@link #setRemoveOnCancelPolicy} is set {@code true} |
25 |
> |
* cancelled tasks are automatically removed from the work queue. |
26 |
|
* |
27 |
|
* <p>While this class inherits from {@link ThreadPoolExecutor}, a few |
28 |
|
* of the inherited tuning methods are not useful for it. In |
123 |
|
private volatile boolean executeExistingDelayedTasksAfterShutdown = true; |
124 |
|
|
125 |
|
/** |
126 |
+ |
* True if ScheduledFutureTask.cancel should remove from queue |
127 |
+ |
*/ |
128 |
+ |
private volatile boolean removeOnCancel = false; |
129 |
+ |
|
130 |
+ |
/** |
131 |
|
* Sequence number to break scheduling ties, and in turn to |
132 |
|
* guarantee FIFO order among tied entries. |
133 |
|
*/ |
236 |
|
} |
237 |
|
|
238 |
|
public boolean cancel(boolean mayInterruptIfRunning) { |
239 |
< |
remove(this); // unconditionally remove |
240 |
< |
return super.cancel(mayInterruptIfRunning); |
239 |
> |
boolean cancelled = super.cancel(mayInterruptIfRunning); |
240 |
> |
if (cancelled && removeOnCancel && heapIndex >= 0) |
241 |
> |
remove(this); |
242 |
> |
return cancelled; |
243 |
|
} |
244 |
|
|
245 |
|
/** |
329 |
|
if (e instanceof RunnableScheduledFuture) { |
330 |
|
RunnableScheduledFuture<?> t = |
331 |
|
(RunnableScheduledFuture<?>)e; |
332 |
< |
if ((t.isPeriodic() ? !keepPeriodic : !keepDelayed)) |
333 |
< |
t.cancel(false); |
332 |
> |
if ((t.isPeriodic() ? !keepPeriodic : !keepDelayed) || |
333 |
> |
t.isCancelled()) { // also remove if already cancelled |
334 |
> |
if (q.remove(t)) |
335 |
> |
t.cancel(false); |
336 |
> |
} |
337 |
|
} |
338 |
|
} |
339 |
|
} |
638 |
|
} |
639 |
|
|
640 |
|
/** |
641 |
+ |
* Sets the policy on whether to cancellation of a a task should |
642 |
+ |
* remove it from the work queue. This value is |
643 |
+ |
* by default {@code false}. |
644 |
+ |
* |
645 |
+ |
* @param value if {@code true}, remove on cancellation, else don't. |
646 |
+ |
* @see #getRemoveOnCancelPolicy |
647 |
+ |
*/ |
648 |
+ |
public void setRemoveOnCancelPolicy(boolean value) { |
649 |
+ |
removeOnCancel = value; |
650 |
+ |
} |
651 |
+ |
|
652 |
+ |
/** |
653 |
+ |
* Gets the policy on whether to cancellation of a a task should |
654 |
+ |
* remove it from the work queue. |
655 |
+ |
* This value is by default {@code false}. |
656 |
+ |
* |
657 |
+ |
* @return {@code true} if cancelled tasks are removed from the queue. |
658 |
+ |
* @see #setRemoveOnCancelPolicy |
659 |
+ |
*/ |
660 |
+ |
public boolean getRemoveOnCancelPolicy() { |
661 |
+ |
return removeOnCancel; |
662 |
+ |
} |
663 |
+ |
|
664 |
+ |
/** |
665 |
|
* Initiates an orderly shutdown in which previously submitted |
666 |
|
* tasks are executed, but no new tasks will be accepted. If the |
667 |
|
* {@code ExecuteExistingDelayedTasksAfterShutdownPolicy} has |