98 |
|
} |
99 |
|
|
100 |
|
public int compareTo(Object other) { |
101 |
+ |
if (other == this) |
102 |
+ |
return 0; |
103 |
|
DelayedTask x = (DelayedTask)other; |
104 |
|
long diff = time - x.time; |
105 |
|
if (diff < 0) |
472 |
|
} |
473 |
|
|
474 |
|
/** |
475 |
+ |
* Removes this task from internal queue if it is present, thus |
476 |
+ |
* causing it not to be run if it has not already started. This |
477 |
+ |
* method may be useful as one part of a cancellation scheme. |
478 |
+ |
* |
479 |
+ |
* @param task the task to remove |
480 |
+ |
* @return true if the task was removed |
481 |
+ |
*/ |
482 |
+ |
public boolean remove(Runnable task) { |
483 |
+ |
if (task instanceof DelayedTask && getQueue().remove(task)) |
484 |
+ |
return true; |
485 |
+ |
|
486 |
+ |
// The task might actually have been wrapped as a DelayedTask |
487 |
+ |
// in execute(), in which case we need to maually traverse |
488 |
+ |
// looking for it. |
489 |
+ |
|
490 |
+ |
DelayedTask wrap = null; |
491 |
+ |
Object[] entries = getQueue().toArray(); |
492 |
+ |
for (int i = 0; i < entries.length; ++i) { |
493 |
+ |
DelayedTask t = (DelayedTask)entries[i]; |
494 |
+ |
Runnable r = t.getRunnable(); |
495 |
+ |
if (task.equals(r)) { |
496 |
+ |
wrap = t; |
497 |
+ |
break; |
498 |
+ |
} |
499 |
+ |
} |
500 |
+ |
entries = null; |
501 |
+ |
return wrap != null && getQueue().remove(wrap); |
502 |
+ |
} |
503 |
+ |
|
504 |
+ |
/** |
505 |
|
* If executed task was periodic, cause the task for the next |
506 |
|
* period to execute. |
507 |
|
* @param r the task (assumed to be a DelayedTask) |
516 |
|
if (next == null) |
517 |
|
return; |
518 |
|
try { |
519 |
< |
execute(next); |
519 |
> |
delayedExecute(next); |
520 |
|
} catch(RejectedExecutionException ex) { |
521 |
|
// lost race to detect shutdown; ignore |
522 |
|
} |