25 |
|
public class ScheduledExecutor extends ThreadPoolExecutor { |
26 |
|
|
27 |
|
/** |
28 |
< |
* A delayed or periodic action. |
28 |
> |
* Sequence number to break scheduling ties, and in turn to |
29 |
> |
* guarantee FIFO order among tied entries. |
30 |
|
*/ |
31 |
< |
public static class DelayedTask extends CancellableTask implements Delayed { |
31 |
< |
/** |
32 |
< |
* Sequence number to break ties, and in turn to guarantee |
33 |
< |
* FIFO order among tied entries. |
34 |
< |
*/ |
35 |
< |
private static final AtomicLong sequencer = new AtomicLong(0); |
31 |
> |
private static final AtomicLong sequencer = new AtomicLong(0); |
32 |
|
|
33 |
+ |
/** |
34 |
+ |
* A delayed or periodic action. |
35 |
+ |
*/ |
36 |
+ |
public class DelayedTask extends CancellableTask implements Delayed { |
37 |
|
private final long sequenceNumber; |
38 |
|
private final long time; |
39 |
|
private final long period; |
40 |
|
|
41 |
|
/** |
42 |
< |
* Creates a one-shot action with given nanosecond delay |
42 |
> |
* Creates a one-shot action with given nanoTime-based trigger time |
43 |
|
*/ |
44 |
|
DelayedTask(Runnable r, long ns) { |
45 |
|
super(r); |
49 |
|
} |
50 |
|
|
51 |
|
/** |
52 |
< |
* Creates a periodic action with given nanosecond delay and period |
52 |
> |
* Creates a periodic action with given nano time and period |
53 |
|
*/ |
54 |
|
DelayedTask(Runnable r, long ns, long period) { |
55 |
|
super(r); |
78 |
|
return 1; |
79 |
|
} |
80 |
|
|
81 |
+ |
public boolean cancel(boolean mayInterruptIfRunning) { |
82 |
+ |
if (!isDone()) |
83 |
+ |
ScheduledExecutor.this.remove(this); |
84 |
+ |
return super.cancel(mayInterruptIfRunning); |
85 |
+ |
} |
86 |
+ |
|
87 |
|
/** |
88 |
|
* Return true if this is a periodic (not a one-shot) action. |
89 |
|
*/ |
103 |
|
* subsequent to current task, or null if non-periodic |
104 |
|
* or canceled. |
105 |
|
*/ |
106 |
< |
public DelayedTask nextTask() { |
106 |
> |
DelayedTask nextTask() { |
107 |
|
if (period <= 0 || isCancelled()) |
108 |
|
return null; |
109 |
|
return new DelayedTask(getRunnable(), time+period, period); |
114 |
|
/** |
115 |
|
* A delayed result-bearing action. |
116 |
|
*/ |
117 |
< |
public static class DelayedFutureTask<V> extends DelayedTask implements Future<V> { |
117 |
> |
public class DelayedFutureTask<V> extends DelayedTask implements Future<V> { |
118 |
|
/** |
119 |
|
* Creates a Future that may trigger after the given delay. |
120 |
|
*/ |