68 |
|
private final long sequenceNumber; |
69 |
|
/** The time the task is enabled to execute in nanoTime units */ |
70 |
|
private long time; |
71 |
< |
/** The delay following next time, or <= 0 if non-periodic */ |
71 |
> |
/** |
72 |
> |
* Period in nanoseconds for repeating tasks. A positive |
73 |
> |
* value indicates fixed-rate execution. A negative value |
74 |
> |
* indicates fixed-delay execution. A value of 0 indicates a |
75 |
> |
* non-repeating task. |
76 |
> |
*/ |
77 |
|
private final long period; |
73 |
– |
/** true if at fixed rate; false if fixed delay */ |
74 |
– |
private final boolean rateBased; |
78 |
|
|
79 |
|
/** |
80 |
|
* Creates a one-shot action with given nanoTime-based trigger time |
83 |
|
super(r, result); |
84 |
|
this.time = ns; |
85 |
|
this.period = 0; |
83 |
– |
rateBased = false; |
86 |
|
this.sequenceNumber = sequencer.getAndIncrement(); |
87 |
|
} |
88 |
|
|
89 |
|
/** |
90 |
|
* Creates a periodic action with given nano time and period |
91 |
|
*/ |
92 |
< |
ScheduledFutureTask(Runnable r, V result, long ns, long period, boolean rateBased) { |
92 |
> |
ScheduledFutureTask(Runnable r, V result, long ns, long period) { |
93 |
|
super(r, result); |
94 |
|
this.time = ns; |
95 |
|
this.period = period; |
94 |
– |
this.rateBased = rateBased; |
96 |
|
this.sequenceNumber = sequencer.getAndIncrement(); |
97 |
|
} |
98 |
|
|
103 |
|
super(callable); |
104 |
|
this.time = ns; |
105 |
|
this.period = 0; |
105 |
– |
rateBased = false; |
106 |
|
this.sequenceNumber = sequencer.getAndIncrement(); |
107 |
|
} |
108 |
|
|
131 |
|
* @return true if periodic |
132 |
|
*/ |
133 |
|
boolean isPeriodic() { |
134 |
< |
return period > 0; |
134 |
> |
return period != 0; |
135 |
|
} |
136 |
|
|
137 |
|
/** |
153 |
|
if (ok && (!down || |
154 |
|
(getContinueExistingPeriodicTasksAfterShutdownPolicy() && |
155 |
|
!isTerminating()))) { |
156 |
< |
time = period + (rateBased ? time : now()); |
156 |
> |
long p = period; |
157 |
> |
if (p > 0) |
158 |
> |
time += p; |
159 |
> |
else |
160 |
> |
time = now() - p; |
161 |
|
ScheduledThreadPoolExecutor.super.getQueue().add(this); |
162 |
|
} |
163 |
|
// This might have been the final executed delayed |
300 |
|
TimeUnit unit) { |
301 |
|
if (callable == null || unit == null) |
302 |
|
throw new NullPointerException(); |
303 |
+ |
if (delay < 0) delay = 0; |
304 |
|
long triggerTime = now() + unit.toNanos(delay); |
305 |
|
ScheduledFutureTask<V> t = |
306 |
|
new ScheduledFutureTask<V>(callable, triggerTime); |
316 |
|
throw new NullPointerException(); |
317 |
|
if (period <= 0) |
318 |
|
throw new IllegalArgumentException(); |
319 |
+ |
if (initialDelay < 0) initialDelay = 0; |
320 |
|
long triggerTime = now() + unit.toNanos(initialDelay); |
321 |
|
ScheduledFutureTask<?> t = |
322 |
|
new ScheduledFutureTask<Object>(command, |
323 |
|
null, |
324 |
|
triggerTime, |
325 |
< |
unit.toNanos(period), |
320 |
< |
true); |
325 |
> |
unit.toNanos(period)); |
326 |
|
delayedExecute(t); |
327 |
|
return t; |
328 |
|
} |
335 |
|
throw new NullPointerException(); |
336 |
|
if (delay <= 0) |
337 |
|
throw new IllegalArgumentException(); |
338 |
+ |
if (initialDelay < 0) initialDelay = 0; |
339 |
|
long triggerTime = now() + unit.toNanos(initialDelay); |
340 |
|
ScheduledFutureTask<?> t = |
341 |
|
new ScheduledFutureTask<Boolean>(command, |
342 |
|
null, |
343 |
|
triggerTime, |
344 |
< |
unit.toNanos(delay), |
339 |
< |
false); |
344 |
> |
unit.toNanos(-delay)); |
345 |
|
delayedExecute(t); |
346 |
|
return t; |
347 |
|
} |
389 |
|
* <tt>false</tt> when already shutdown. This value is by default |
390 |
|
* false. |
391 |
|
* @param value if true, continue after shutdown, else don't. |
392 |
+ |
* @see #getExecuteExistingDelayedTasksAfterShutdownPolicy |
393 |
|
*/ |
394 |
|
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value) { |
395 |
|
continueExistingPeriodicTasksAfterShutdown = value; |
405 |
|
* to <tt>false</tt> when already shutdown. This value is by |
406 |
|
* default false. |
407 |
|
* @return true if will continue after shutdown. |
408 |
+ |
* @see #setContinueExistingPeriodicTasksAfterShutdownPolicy |
409 |
|
*/ |
410 |
|
public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy() { |
411 |
|
return continueExistingPeriodicTasksAfterShutdown; |
419 |
|
* <tt>false</tt> when already shutdown. This value is by default |
420 |
|
* true. |
421 |
|
* @param value if true, execute after shutdown, else don't. |
422 |
+ |
* @see #getExecuteExistingDelayedTasksAfterShutdownPolicy |
423 |
|
*/ |
424 |
|
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) { |
425 |
|
executeExistingDelayedTasksAfterShutdown = value; |
435 |
|
* <tt>false</tt> when already shutdown. This value is by default |
436 |
|
* true. |
437 |
|
* @return true if will execute after shutdown. |
438 |
+ |
* @see #setExecuteExistingDelayedTasksAfterShutdownPolicy |
439 |
|
*/ |
440 |
|
public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy() { |
441 |
|
return executeExistingDelayedTasksAfterShutdown; |