76 |
|
private long sequenceNumber; |
77 |
|
/** The time the task is enabled to execute in nanoTime units */ |
78 |
|
private long time; |
79 |
< |
/** The delay forllowing next time, or <= 0 if non-periodic */ |
80 |
< |
private long period; |
79 |
> |
/** The delay following next time, or <= 0 if non-periodic */ |
80 |
> |
private final long period; |
81 |
|
/** true if at fixed rate; false if fixed delay */ |
82 |
|
private final boolean rateBased; |
83 |
|
|
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
+ |
* Creates a one-shot action with given nanoTime-based trigger time |
97 |
+ |
* but does not establish the action (need for Future subclass). |
98 |
+ |
*/ |
99 |
+ |
ScheduledCancellableTask(long ns) { |
100 |
+ |
super(); |
101 |
+ |
this.time = ns; |
102 |
+ |
this.period = 0; |
103 |
+ |
rateBased = false; |
104 |
+ |
this.sequenceNumber = sequencer.getAndIncrement(); |
105 |
+ |
} |
106 |
+ |
|
107 |
+ |
/** |
108 |
|
* Creates a periodic action with given nano time and period |
109 |
|
*/ |
110 |
|
ScheduledCancellableTask(Runnable r, long ns, long period, boolean rateBased) { |
157 |
|
} |
158 |
|
|
159 |
|
/** |
160 |
< |
* Return a new ScheduledCancellable that will trigger in the period |
161 |
< |
* subsequent to current task, or null if non-periodic |
162 |
< |
* or canceled. |
160 |
> |
* Override run method not to setDone if periodic |
161 |
> |
*/ |
162 |
> |
public void run() { |
163 |
> |
if (setRunning()) { |
164 |
> |
try { |
165 |
> |
getRunnable().run(); |
166 |
> |
} finally { |
167 |
> |
if (!isPeriodic()) |
168 |
> |
setDone(); |
169 |
> |
} |
170 |
> |
} |
171 |
> |
} |
172 |
> |
|
173 |
> |
/** |
174 |
> |
* Return a new ScheduledCancellable task (which may be the |
175 |
> |
* this task) that will trigger in the period subsequent to |
176 |
> |
* current task, or null if non-periodic or cancelled. |
177 |
|
*/ |
178 |
|
ScheduledCancellableTask nextTask() { |
179 |
< |
if (period <= 0 || isCancelled()) |
179 |
> |
if (period <= 0 || !reset()) |
180 |
|
return null; |
181 |
< |
long nextTime = period + (rateBased ? time : System.nanoTime()); |
182 |
< |
this.time = nextTime; |
157 |
< |
this.sequenceNumber = sequencer.getAndIncrement(); |
158 |
< |
reset(); |
181 |
> |
time = period + (rateBased ? time : System.nanoTime()); |
182 |
> |
sequenceNumber = sequencer.getAndIncrement(); |
183 |
|
return this; |
184 |
|
} |
185 |
|
} |
191 |
|
* Creates a ScheduledFuture that may trigger after the given delay. |
192 |
|
*/ |
193 |
|
ScheduledFutureTask(Callable<V> callable, long triggerTime) { |
194 |
< |
// must set after super ctor call to use inner class |
195 |
< |
super(null, triggerTime); |
194 |
> |
// must set callable after super ctor call to use inner class |
195 |
> |
super(triggerTime); |
196 |
|
setRunnable(new InnerCancellableFuture<V>(callable)); |
197 |
|
} |
198 |
|
|
278 |
|
* even if they are idle. |
279 |
|
* @param threadFactory the factory to use when the executor |
280 |
|
* creates a new thread. |
281 |
+ |
* @throws NullPointerException if threadFactory is null |
282 |
|
*/ |
283 |
|
public ScheduledExecutor(int corePoolSize, |
284 |
|
ThreadFactory threadFactory) { |
293 |
|
* even if they are idle. |
294 |
|
* @param handler the handler to use when execution is blocked |
295 |
|
* because the thread bounds and queue capacities are reached. |
296 |
+ |
* @throws NullPointerException if handler is null |
297 |
|
*/ |
298 |
|
public ScheduledExecutor(int corePoolSize, |
299 |
|
RejectedExecutionHandler handler) { |
310 |
|
* creates a new thread. |
311 |
|
* @param handler the handler to use when execution is blocked |
312 |
|
* because the thread bounds and queue capacities are reached. |
313 |
+ |
* @throws NullPointerException if threadFactory or handler is null |
314 |
|
*/ |
315 |
|
public ScheduledExecutor(int corePoolSize, |
316 |
|
ThreadFactory threadFactory, |
359 |
|
* after the given initial delay, and subsequently with the given |
360 |
|
* period; that is executions will commence after |
361 |
|
* <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then |
362 |
< |
* <tt>initialDelay + 2 * period</tt>, and so on. |
362 |
> |
* <tt>initialDelay + 2 * period</tt>, and so on. The |
363 |
> |
* task will only terminate via cancellation. |
364 |
|
* @param command the task to execute. |
365 |
|
* @param initialDelay the time to delay first execution. |
366 |
|
* @param period the period between successive executions. |
379 |
|
return t; |
380 |
|
} |
381 |
|
|
354 |
– |
|
382 |
|
/** |
383 |
|
* Creates and executes a periodic action that becomes enabled first |
384 |
|
* after the given initial delay, and and subsequently with the |
385 |
|
* given delay between the termination of one execution and the |
386 |
< |
* commencement of the next. |
386 |
> |
* commencement of the next. |
387 |
> |
* The task will only terminate via cancellation. |
388 |
|
* @param command the task to execute. |
389 |
|
* @param initialDelay the time to delay first execution. |
390 |
|
* @param delay the delay between the termination of one |