1 |
tim |
1.1 |
/* |
2 |
dl |
1.4 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain. Use, modify, and |
4 |
|
|
* redistribute this code in any way without acknowledgement. |
5 |
tim |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
dl |
1.4 |
import java.util.concurrent.atomic.*; |
9 |
dl |
1.2 |
import java.util.*; |
10 |
tim |
1.1 |
|
11 |
|
|
/** |
12 |
dl |
1.25 |
* An {@link Executor} that can schedule commands to run after a given |
13 |
dl |
1.7 |
* delay, or to execute periodically. This class is preferable to |
14 |
dl |
1.25 |
* {@link java.util.Timer} when multiple worker threads are needed, |
15 |
tim |
1.1 |
* or when the additional flexibility or capabilities of |
16 |
dl |
1.25 |
* {@link ThreadPoolExecutor} (which this class extends) are |
17 |
dl |
1.7 |
* required. |
18 |
|
|
* |
19 |
|
|
* <p> The <tt>schedule</tt> methods create tasks with various delays |
20 |
|
|
* and return a task object that can be used to cancel or check |
21 |
|
|
* execution. The <tt>scheduleAtFixedRate</tt> and |
22 |
|
|
* <tt>scheduleWithFixedDelay</tt> methods create and execute tasks |
23 |
|
|
* that run periodically until cancelled. Commands submitted using |
24 |
|
|
* the <tt>execute</tt> method are scheduled with a requested delay of |
25 |
|
|
* zero. |
26 |
|
|
* |
27 |
|
|
* <p> Delayed tasks execute no sooner than they are enabled, but |
28 |
dl |
1.22 |
* without any real-time guarantees about when, after they are enabled, |
29 |
dl |
1.7 |
* they will commence. Tasks tied for the same execution time are |
30 |
dl |
1.22 |
* enabled in first-in-first-out (FIFO) order of submission. |
31 |
|
|
* |
32 |
|
|
* <p>All <t>schedule</tt> methods accept <em>relative</em> delays and |
33 |
|
|
* periods as arguments, not absolute times or dates. It is a simple |
34 |
|
|
* matter to transform an absolute time represented as a |
35 |
dl |
1.25 |
* {@link java.util.Date}, to the required form. For example, to |
36 |
dl |
1.22 |
* schedule at a certain future <tt>date</tt>, you can use: |
37 |
dl |
1.23 |
* <tt>schedule(task, date.getTime() - System.currentTimeMillis(), |
38 |
dl |
1.22 |
* TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a |
39 |
|
|
* relative delay need not coincide with the current <tt>Date</tt> at |
40 |
|
|
* which the task is enabled due to network time synchronization |
41 |
|
|
* protocols, clock drift, or other factors. |
42 |
tim |
1.1 |
* |
43 |
dl |
1.13 |
* <p>While this class inherits from {@link ThreadPoolExecutor}, a few |
44 |
|
|
* of the inherited tuning methods are not especially useful for |
45 |
|
|
* it. In particular, because a <tt>ScheduledExecutor</tt> always acts |
46 |
|
|
* as a fixed-sized pool using <tt>corePoolSize</tt> threads and an |
47 |
|
|
* unbounded queue, adjustments to <tt>maximumPoolSize</tt> have no |
48 |
|
|
* useful effect. |
49 |
|
|
* |
50 |
tim |
1.1 |
* @since 1.5 |
51 |
dl |
1.9 |
* @author Doug Lea |
52 |
tim |
1.1 |
*/ |
53 |
|
|
public class ScheduledExecutor extends ThreadPoolExecutor { |
54 |
|
|
|
55 |
dl |
1.4 |
/** |
56 |
dl |
1.18 |
* False if should cancel/suppress periodic tasks on shutdown. |
57 |
|
|
*/ |
58 |
|
|
private volatile boolean continueExistingPeriodicTasksAfterShutdown; |
59 |
|
|
|
60 |
|
|
/** |
61 |
|
|
* False if should cancel non-periodic tasks on shutdown. |
62 |
|
|
*/ |
63 |
|
|
private volatile boolean executeExistingDelayedTasksAfterShutdown = true; |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
/** |
67 |
dl |
1.5 |
* Sequence number to break scheduling ties, and in turn to |
68 |
|
|
* guarantee FIFO order among tied entries. |
69 |
|
|
*/ |
70 |
|
|
private static final AtomicLong sequencer = new AtomicLong(0); |
71 |
tim |
1.21 |
|
72 |
|
|
private static class ScheduledCancellableTask |
73 |
|
|
extends CancellableTask implements ScheduledCancellable { |
74 |
|
|
|
75 |
dl |
1.9 |
/** Sequence number to break ties FIFO */ |
76 |
dl |
1.4 |
private final long sequenceNumber; |
77 |
dl |
1.9 |
/** The time the task is enabled to execute in nanoTime units */ |
78 |
dl |
1.4 |
private final long time; |
79 |
dl |
1.9 |
/** The delay forllowing next time, or <= 0 if non-periodic */ |
80 |
dl |
1.4 |
private final long period; |
81 |
dl |
1.9 |
/** true if at fixed rate; false if fixed delay */ |
82 |
|
|
private final boolean rateBased; |
83 |
|
|
|
84 |
dl |
1.4 |
/** |
85 |
dl |
1.5 |
* Creates a one-shot action with given nanoTime-based trigger time |
86 |
dl |
1.4 |
*/ |
87 |
tim |
1.21 |
ScheduledCancellableTask(Runnable r, long ns) { |
88 |
dl |
1.4 |
super(r); |
89 |
|
|
this.time = ns; |
90 |
|
|
this.period = 0; |
91 |
dl |
1.7 |
rateBased = false; |
92 |
dl |
1.4 |
this.sequenceNumber = sequencer.getAndIncrement(); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
/** |
96 |
dl |
1.5 |
* Creates a periodic action with given nano time and period |
97 |
dl |
1.4 |
*/ |
98 |
tim |
1.21 |
ScheduledCancellableTask(Runnable r, long ns, long period, boolean rateBased) { |
99 |
dl |
1.4 |
super(r); |
100 |
|
|
if (period <= 0) |
101 |
|
|
throw new IllegalArgumentException(); |
102 |
|
|
this.time = ns; |
103 |
|
|
this.period = period; |
104 |
dl |
1.7 |
this.rateBased = rateBased; |
105 |
dl |
1.4 |
this.sequenceNumber = sequencer.getAndIncrement(); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
|
109 |
|
|
public long getDelay(TimeUnit unit) { |
110 |
dl |
1.12 |
long d = unit.convert(time - System.nanoTime(), |
111 |
dl |
1.7 |
TimeUnit.NANOSECONDS); |
112 |
dl |
1.12 |
return d; |
113 |
dl |
1.4 |
} |
114 |
|
|
|
115 |
|
|
public int compareTo(Object other) { |
116 |
dl |
1.18 |
if (other == this) // compare zero ONLY if same object |
117 |
dl |
1.15 |
return 0; |
118 |
tim |
1.21 |
ScheduledCancellableTask x = (ScheduledCancellableTask)other; |
119 |
dl |
1.4 |
long diff = time - x.time; |
120 |
|
|
if (diff < 0) |
121 |
|
|
return -1; |
122 |
|
|
else if (diff > 0) |
123 |
|
|
return 1; |
124 |
|
|
else if (sequenceNumber < x.sequenceNumber) |
125 |
|
|
return -1; |
126 |
|
|
else |
127 |
|
|
return 1; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/** |
131 |
|
|
* Return true if this is a periodic (not a one-shot) action. |
132 |
dl |
1.9 |
* @return true if periodic |
133 |
dl |
1.4 |
*/ |
134 |
|
|
public boolean isPeriodic() { |
135 |
|
|
return period > 0; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
/** |
139 |
tim |
1.11 |
* Returns the period, or zero if non-periodic. |
140 |
|
|
* |
141 |
dl |
1.9 |
* @return the period |
142 |
dl |
1.4 |
*/ |
143 |
|
|
public long getPeriod(TimeUnit unit) { |
144 |
|
|
return unit.convert(period, TimeUnit.NANOSECONDS); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
/** |
148 |
tim |
1.21 |
* Return a new ScheduledCancellable that will trigger in the period |
149 |
dl |
1.4 |
* subsequent to current task, or null if non-periodic |
150 |
|
|
* or canceled. |
151 |
|
|
*/ |
152 |
tim |
1.21 |
ScheduledCancellableTask nextTask() { |
153 |
dl |
1.4 |
if (period <= 0 || isCancelled()) |
154 |
|
|
return null; |
155 |
dl |
1.10 |
long nextTime = period + (rateBased ? time : System.nanoTime()); |
156 |
tim |
1.21 |
return new ScheduledCancellableTask(getRunnable(), nextTime, period, rateBased); |
157 |
dl |
1.4 |
} |
158 |
|
|
} |
159 |
tim |
1.21 |
|
160 |
|
|
private static class ScheduledFutureTask<V> |
161 |
|
|
extends ScheduledCancellableTask implements ScheduledFuture<V> { |
162 |
|
|
|
163 |
dl |
1.4 |
/** |
164 |
tim |
1.21 |
* Creates a ScheduledFuture that may trigger after the given delay. |
165 |
dl |
1.4 |
*/ |
166 |
tim |
1.21 |
ScheduledFutureTask(Callable<V> callable, long triggerTime) { |
167 |
dl |
1.4 |
// must set after super ctor call to use inner class |
168 |
dl |
1.17 |
super(null, triggerTime); |
169 |
dl |
1.4 |
setRunnable(new InnerCancellableFuture<V>(callable)); |
170 |
dl |
1.2 |
} |
171 |
|
|
|
172 |
dl |
1.4 |
public V get() throws InterruptedException, ExecutionException { |
173 |
|
|
return ((InnerCancellableFuture<V>)getRunnable()).get(); |
174 |
dl |
1.2 |
} |
175 |
|
|
|
176 |
dl |
1.4 |
public V get(long timeout, TimeUnit unit) |
177 |
|
|
throws InterruptedException, ExecutionException, TimeoutException { |
178 |
|
|
return ((InnerCancellableFuture<V>)getRunnable()).get(timeout, unit); |
179 |
dl |
1.2 |
} |
180 |
tim |
1.1 |
|
181 |
dl |
1.4 |
protected void set(V v) { |
182 |
|
|
((InnerCancellableFuture<V>)getRunnable()).set(v); |
183 |
dl |
1.2 |
} |
184 |
tim |
1.1 |
|
185 |
dl |
1.4 |
protected void setException(Throwable t) { |
186 |
|
|
((InnerCancellableFuture<V>)getRunnable()).setException(t); |
187 |
|
|
} |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
|
191 |
|
|
/** |
192 |
|
|
* An annoying wrapper class to convince generics compiler to |
193 |
tim |
1.21 |
* use a DelayQueue<ScheduledCancellableTask> as a BlockingQueue<Runnable> |
194 |
dl |
1.4 |
*/ |
195 |
tim |
1.21 |
private static class DelayedWorkQueue |
196 |
|
|
extends AbstractCollection<Runnable> implements BlockingQueue<Runnable> { |
197 |
|
|
|
198 |
|
|
private final DelayQueue<ScheduledCancellableTask> dq = new DelayQueue<ScheduledCancellableTask>(); |
199 |
dl |
1.4 |
public Runnable poll() { return dq.poll(); } |
200 |
|
|
public Runnable peek() { return dq.peek(); } |
201 |
|
|
public Runnable take() throws InterruptedException { return dq.take(); } |
202 |
|
|
public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException { |
203 |
|
|
return dq.poll(timeout, unit); |
204 |
dl |
1.2 |
} |
205 |
dl |
1.12 |
|
206 |
tim |
1.21 |
public boolean add(Runnable x) { return dq.add((ScheduledCancellableTask)x); } |
207 |
|
|
public boolean offer(Runnable x) { return dq.offer((ScheduledCancellableTask)x); } |
208 |
dl |
1.4 |
public void put(Runnable x) throws InterruptedException { |
209 |
tim |
1.21 |
dq.put((ScheduledCancellableTask)x); |
210 |
dl |
1.2 |
} |
211 |
dl |
1.4 |
public boolean offer(Runnable x, long timeout, TimeUnit unit) throws InterruptedException { |
212 |
tim |
1.21 |
return dq.offer((ScheduledCancellableTask)x, timeout, unit); |
213 |
dl |
1.2 |
} |
214 |
dl |
1.12 |
|
215 |
|
|
public Runnable remove() { return dq.remove(); } |
216 |
|
|
public Runnable element() { return dq.element(); } |
217 |
|
|
public void clear() { dq.clear(); } |
218 |
|
|
|
219 |
dl |
1.4 |
public int remainingCapacity() { return dq.remainingCapacity(); } |
220 |
|
|
public boolean remove(Object x) { return dq.remove(x); } |
221 |
|
|
public boolean contains(Object x) { return dq.contains(x); } |
222 |
|
|
public int size() { return dq.size(); } |
223 |
tim |
1.11 |
public boolean isEmpty() { return dq.isEmpty(); } |
224 |
dl |
1.4 |
public Iterator<Runnable> iterator() { |
225 |
|
|
return new Iterator<Runnable>() { |
226 |
tim |
1.21 |
private Iterator<ScheduledCancellableTask> it = dq.iterator(); |
227 |
dl |
1.4 |
public boolean hasNext() { return it.hasNext(); } |
228 |
|
|
public Runnable next() { return it.next(); } |
229 |
|
|
public void remove() { it.remove(); } |
230 |
|
|
}; |
231 |
tim |
1.1 |
} |
232 |
dl |
1.4 |
} |
233 |
tim |
1.1 |
|
234 |
dl |
1.4 |
/** |
235 |
|
|
* Creates a new ScheduledExecutor with the given initial parameters. |
236 |
|
|
* |
237 |
|
|
* @param corePoolSize the number of threads to keep in the pool, |
238 |
|
|
* even if they are idle. |
239 |
|
|
*/ |
240 |
|
|
public ScheduledExecutor(int corePoolSize) { |
241 |
|
|
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
242 |
|
|
new DelayedWorkQueue()); |
243 |
|
|
} |
244 |
tim |
1.1 |
|
245 |
dl |
1.4 |
/** |
246 |
|
|
* Creates a new ScheduledExecutor with the given initial parameters. |
247 |
|
|
* |
248 |
|
|
* @param corePoolSize the number of threads to keep in the pool, |
249 |
|
|
* even if they are idle. |
250 |
|
|
* @param threadFactory the factory to use when the executor |
251 |
|
|
* creates a new thread. |
252 |
|
|
*/ |
253 |
|
|
public ScheduledExecutor(int corePoolSize, |
254 |
|
|
ThreadFactory threadFactory) { |
255 |
|
|
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
256 |
|
|
new DelayedWorkQueue(), threadFactory); |
257 |
tim |
1.1 |
} |
258 |
|
|
|
259 |
dl |
1.4 |
/** |
260 |
|
|
* Creates a new ScheduledExecutor with the given initial parameters. |
261 |
|
|
* |
262 |
|
|
* @param corePoolSize the number of threads to keep in the pool, |
263 |
|
|
* even if they are idle. |
264 |
|
|
* @param handler the handler to use when execution is blocked |
265 |
|
|
* because the thread bounds and queue capacities are reached. |
266 |
|
|
*/ |
267 |
|
|
public ScheduledExecutor(int corePoolSize, |
268 |
|
|
RejectedExecutionHandler handler) { |
269 |
|
|
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
270 |
|
|
new DelayedWorkQueue(), handler); |
271 |
|
|
} |
272 |
dl |
1.2 |
|
273 |
tim |
1.1 |
/** |
274 |
dl |
1.2 |
* Creates a new ScheduledExecutor with the given initial parameters. |
275 |
|
|
* |
276 |
|
|
* @param corePoolSize the number of threads to keep in the pool, |
277 |
|
|
* even if they are idle. |
278 |
dl |
1.4 |
* @param threadFactory the factory to use when the executor |
279 |
|
|
* creates a new thread. |
280 |
|
|
* @param handler the handler to use when execution is blocked |
281 |
|
|
* because the thread bounds and queue capacities are reached. |
282 |
tim |
1.1 |
*/ |
283 |
dl |
1.4 |
public ScheduledExecutor(int corePoolSize, |
284 |
|
|
ThreadFactory threadFactory, |
285 |
|
|
RejectedExecutionHandler handler) { |
286 |
dl |
1.2 |
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
287 |
dl |
1.4 |
new DelayedWorkQueue(), threadFactory, handler); |
288 |
dl |
1.13 |
} |
289 |
|
|
|
290 |
|
|
/** |
291 |
|
|
* Specialized variant of ThreadPoolExecutor.execute for delayed tasks. |
292 |
|
|
*/ |
293 |
dl |
1.18 |
private void delayedExecute(Runnable command) { |
294 |
dl |
1.13 |
if (isShutdown()) { |
295 |
|
|
reject(command); |
296 |
|
|
return; |
297 |
|
|
} |
298 |
dl |
1.18 |
// Prestart a thread if necessary. We cannot prestart it |
299 |
|
|
// running the task because the task (probably) shouldn't be |
300 |
|
|
// run yet, so thread will just idle until delay elapses. |
301 |
dl |
1.13 |
if (getPoolSize() < getCorePoolSize()) |
302 |
dl |
1.18 |
prestartCoreThread(); |
303 |
dl |
1.13 |
|
304 |
dl |
1.20 |
super.getQueue().offer(command); |
305 |
dl |
1.4 |
} |
306 |
|
|
|
307 |
|
|
/** |
308 |
dl |
1.7 |
* Creates and executes a one-shot action that becomes enabled after |
309 |
dl |
1.4 |
* the given delay. |
310 |
dl |
1.6 |
* @param command the task to execute. |
311 |
|
|
* @param delay the time from now to delay execution. |
312 |
|
|
* @param unit the time unit of the delay parameter. |
313 |
|
|
* @return a handle that can be used to cancel the task. |
314 |
dl |
1.17 |
* @throws RejectedExecutionException if task cannot be scheduled |
315 |
|
|
* for execution because the executor has been shut down. |
316 |
dl |
1.4 |
*/ |
317 |
|
|
|
318 |
tim |
1.21 |
public ScheduledCancellable schedule(Runnable command, long delay, TimeUnit unit) { |
319 |
dl |
1.17 |
long triggerTime = System.nanoTime() + unit.toNanos(delay); |
320 |
tim |
1.21 |
ScheduledCancellableTask t = new ScheduledCancellableTask(command, triggerTime); |
321 |
dl |
1.13 |
delayedExecute(t); |
322 |
dl |
1.4 |
return t; |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
/** |
326 |
dl |
1.7 |
* Creates and executes a periodic action that becomes enabled first |
327 |
dl |
1.4 |
* after the given initial delay, and subsequently with the given |
328 |
dl |
1.7 |
* period; that is executions will commence after |
329 |
|
|
* <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then |
330 |
|
|
* <tt>initialDelay + 2 * period</tt>, and so on. |
331 |
dl |
1.6 |
* @param command the task to execute. |
332 |
|
|
* @param initialDelay the time to delay first execution. |
333 |
|
|
* @param period the period between successive executions. |
334 |
|
|
* @param unit the time unit of the delay and period parameters |
335 |
|
|
* @return a handle that can be used to cancel the task. |
336 |
|
|
* @throws RejectedExecutionException if task cannot be scheduled |
337 |
|
|
* for execution because the executor has been shut down. |
338 |
dl |
1.4 |
*/ |
339 |
tim |
1.21 |
public ScheduledCancellable scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { |
340 |
dl |
1.17 |
long triggerTime = System.nanoTime() + unit.toNanos(initialDelay); |
341 |
tim |
1.21 |
ScheduledCancellableTask t = new ScheduledCancellableTask(command, |
342 |
dl |
1.17 |
triggerTime, |
343 |
|
|
unit.toNanos(period), |
344 |
|
|
true); |
345 |
dl |
1.13 |
delayedExecute(t); |
346 |
dl |
1.4 |
return t; |
347 |
|
|
} |
348 |
|
|
|
349 |
dl |
1.7 |
|
350 |
|
|
/** |
351 |
|
|
* Creates and executes a periodic action that becomes enabled first |
352 |
|
|
* after the given initial delay, and and subsequently with the |
353 |
|
|
* given delay between the termination of one execution and the |
354 |
|
|
* commencement of the next. |
355 |
|
|
* @param command the task to execute. |
356 |
|
|
* @param initialDelay the time to delay first execution. |
357 |
|
|
* @param delay the delay between the termination of one |
358 |
|
|
* execution and the commencement of the next. |
359 |
|
|
* @param unit the time unit of the delay and delay parameters |
360 |
|
|
* @return a handle that can be used to cancel the task. |
361 |
|
|
* @throws RejectedExecutionException if task cannot be scheduled |
362 |
|
|
* for execution because the executor has been shut down. |
363 |
|
|
*/ |
364 |
tim |
1.21 |
public ScheduledCancellable scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { |
365 |
dl |
1.17 |
long triggerTime = System.nanoTime() + unit.toNanos(initialDelay); |
366 |
tim |
1.21 |
ScheduledCancellableTask t = new ScheduledCancellableTask(command, |
367 |
dl |
1.17 |
triggerTime, |
368 |
|
|
unit.toNanos(delay), |
369 |
|
|
false); |
370 |
dl |
1.13 |
delayedExecute(t); |
371 |
dl |
1.7 |
return t; |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
/** |
375 |
tim |
1.21 |
* Creates and executes a ScheduledFuture that becomes enabled after the |
376 |
dl |
1.7 |
* given delay. |
377 |
dl |
1.6 |
* @param callable the function to execute. |
378 |
|
|
* @param delay the time from now to delay execution. |
379 |
|
|
* @param unit the time unit of the delay parameter. |
380 |
tim |
1.21 |
* @return a ScheduledFuture that can be used to extract result or cancel. |
381 |
dl |
1.6 |
* @throws RejectedExecutionException if task cannot be scheduled |
382 |
|
|
* for execution because the executor has been shut down. |
383 |
dl |
1.4 |
*/ |
384 |
tim |
1.21 |
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { |
385 |
dl |
1.17 |
long triggerTime = System.nanoTime() + unit.toNanos(delay); |
386 |
tim |
1.21 |
ScheduledFutureTask<V> t = new ScheduledFutureTask<V>(callable, triggerTime); |
387 |
dl |
1.13 |
delayedExecute(t); |
388 |
dl |
1.4 |
return t; |
389 |
|
|
} |
390 |
|
|
|
391 |
|
|
/** |
392 |
dl |
1.16 |
* Execute command with zero required delay. This has effect |
393 |
|
|
* equivalent to <tt>schedule(command, 0, anyUnit)</tt>. Note |
394 |
|
|
* that inspections of the queue and of the list returned by |
395 |
|
|
* <tt>shutdownNow</tt> will access the zero-delayed |
396 |
dl |
1.25 |
* {@link ScheduledCancellable}, not the <tt>command</tt> itself. |
397 |
tim |
1.11 |
* |
398 |
dl |
1.6 |
* @param command the task to execute |
399 |
|
|
* @throws RejectedExecutionException at discretion of |
400 |
|
|
* <tt>RejectedExecutionHandler</tt>, if task cannot be accepted |
401 |
|
|
* for execution because the executor has been shut down. |
402 |
dl |
1.4 |
*/ |
403 |
|
|
public void execute(Runnable command) { |
404 |
|
|
schedule(command, 0, TimeUnit.NANOSECONDS); |
405 |
tim |
1.1 |
} |
406 |
|
|
|
407 |
dl |
1.18 |
|
408 |
|
|
/** |
409 |
|
|
* Set policy on whether to continue executing existing periodic |
410 |
|
|
* tasks even when this executor has been <tt>shutdown</tt>. In |
411 |
|
|
* this case, these tasks will only terminate upon |
412 |
|
|
* <tt>shutdownNow</tt>, or after setting the policy to |
413 |
|
|
* <tt>false</tt> when already shutdown. This value is by default |
414 |
|
|
* false. |
415 |
|
|
* @param value if true, continue after shutdown, else don't. |
416 |
|
|
*/ |
417 |
|
|
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value) { |
418 |
|
|
continueExistingPeriodicTasksAfterShutdown = value; |
419 |
|
|
if (!value && isShutdown()) |
420 |
|
|
cancelUnwantedTasks(); |
421 |
|
|
} |
422 |
|
|
|
423 |
|
|
/** |
424 |
|
|
* Get the policy on whether to continue executing existing |
425 |
|
|
* periodic tasks even when this executor has been |
426 |
|
|
* <tt>shutdown</tt>. In this case, these tasks will only |
427 |
|
|
* terminate upon <tt>shutdownNow</tt> or after setting the policy |
428 |
|
|
* to <tt>false</tt> when already shutdown. This value is by |
429 |
|
|
* default false. |
430 |
|
|
* @return true if will continue after shutdown. |
431 |
|
|
*/ |
432 |
|
|
public boolean getContinueExistingPeriodicTasksAfterShutdownPolicy() { |
433 |
|
|
return continueExistingPeriodicTasksAfterShutdown; |
434 |
|
|
} |
435 |
|
|
|
436 |
|
|
/** |
437 |
|
|
* Set policy on whether to execute existing delayed |
438 |
|
|
* tasks even when this executor has been <tt>shutdown</tt>. In |
439 |
|
|
* this case, these tasks will only terminate upon |
440 |
|
|
* <tt>shutdownNow</tt>, or after setting the policy to |
441 |
|
|
* <tt>false</tt> when already shutdown. This value is by default |
442 |
|
|
* true. |
443 |
|
|
* @param value if true, execute after shutdown, else don't. |
444 |
|
|
*/ |
445 |
|
|
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) { |
446 |
|
|
executeExistingDelayedTasksAfterShutdown = value; |
447 |
|
|
if (!value && isShutdown()) |
448 |
|
|
cancelUnwantedTasks(); |
449 |
|
|
} |
450 |
|
|
|
451 |
|
|
/** |
452 |
dl |
1.24 |
* Get policy on whether to execute existing delayed |
453 |
dl |
1.18 |
* tasks even when this executor has been <tt>shutdown</tt>. In |
454 |
|
|
* this case, these tasks will only terminate upon |
455 |
|
|
* <tt>shutdownNow</tt>, or after setting the policy to |
456 |
|
|
* <tt>false</tt> when already shutdown. This value is by default |
457 |
|
|
* true. |
458 |
|
|
* @return true if will execute after shutdown. |
459 |
|
|
*/ |
460 |
|
|
public boolean getExecuteExistingDelayedTasksAfterShutdownPolicy() { |
461 |
|
|
return executeExistingDelayedTasksAfterShutdown; |
462 |
|
|
} |
463 |
|
|
|
464 |
|
|
/** |
465 |
|
|
* Cancel and clear the queue of all tasks that should not be run |
466 |
|
|
* due to shutdown policy. |
467 |
|
|
*/ |
468 |
|
|
private void cancelUnwantedTasks() { |
469 |
|
|
boolean keepDelayed = getExecuteExistingDelayedTasksAfterShutdownPolicy(); |
470 |
|
|
boolean keepPeriodic = getContinueExistingPeriodicTasksAfterShutdownPolicy(); |
471 |
|
|
if (!keepDelayed && !keepPeriodic) |
472 |
dl |
1.20 |
super.getQueue().clear(); |
473 |
dl |
1.18 |
else if (keepDelayed || keepPeriodic) { |
474 |
dl |
1.20 |
Object[] entries = super.getQueue().toArray(); |
475 |
dl |
1.18 |
for (int i = 0; i < entries.length; ++i) { |
476 |
tim |
1.21 |
ScheduledCancellableTask t = (ScheduledCancellableTask)entries[i]; |
477 |
dl |
1.18 |
if (t.isPeriodic()? !keepPeriodic : !keepDelayed) |
478 |
|
|
t.cancel(false); |
479 |
|
|
} |
480 |
|
|
entries = null; |
481 |
|
|
purge(); |
482 |
|
|
} |
483 |
|
|
} |
484 |
|
|
|
485 |
|
|
/** |
486 |
|
|
* Initiates an orderly shutdown in which previously submitted |
487 |
|
|
* tasks are executed, but no new tasks will be accepted. If the |
488 |
|
|
* <tt>ExecuteExistingDelayedTasksAfterShutdownPolicy</tt> has |
489 |
|
|
* been set <tt>false</tt>, existing delayed tasks whose delays |
490 |
|
|
* have not yet elapsed are cancelled. And unless the |
491 |
dl |
1.19 |
* <tt>ContinueExistingPeriodicTasksAfterShutdownPolicy</tt> hase |
492 |
dl |
1.18 |
* been set <tt>true</tt>, future executions of existing periodic |
493 |
|
|
* tasks will be cancelled. |
494 |
|
|
*/ |
495 |
|
|
public void shutdown() { |
496 |
|
|
cancelUnwantedTasks(); |
497 |
|
|
super.shutdown(); |
498 |
|
|
} |
499 |
dl |
1.20 |
|
500 |
|
|
/** |
501 |
|
|
* Attempts to stop all actively executing tasks, halts the |
502 |
|
|
* processing of waiting tasks, and returns a list of the tasks that were |
503 |
|
|
* awaiting execution. |
504 |
|
|
* |
505 |
|
|
* <p>There are no guarantees beyond best-effort attempts to stop |
506 |
|
|
* processing actively executing tasks. This implementations |
507 |
|
|
* cancels via {@link Thread#interrupt}, so if any tasks mask or |
508 |
|
|
* fail to respond to interrupts, they may never terminate. |
509 |
|
|
* |
510 |
|
|
* @return list of tasks that never commenced execution. Each |
511 |
dl |
1.25 |
* element of this list is a {@link ScheduledCancellable}, |
512 |
|
|
* including those tasks submitted using <tt>execute</tt> which |
513 |
|
|
* are for scheduling purposes used as the basis of a zero-delay |
514 |
|
|
* <tt>ScheduledCancellable</tt>. |
515 |
dl |
1.20 |
*/ |
516 |
|
|
public List shutdownNow() { |
517 |
|
|
return super.shutdownNow(); |
518 |
|
|
} |
519 |
dl |
1.18 |
|
520 |
dl |
1.4 |
/** |
521 |
dl |
1.15 |
* Removes this task from internal queue if it is present, thus |
522 |
|
|
* causing it not to be run if it has not already started. This |
523 |
|
|
* method may be useful as one part of a cancellation scheme. |
524 |
|
|
* |
525 |
|
|
* @param task the task to remove |
526 |
|
|
* @return true if the task was removed |
527 |
|
|
*/ |
528 |
|
|
public boolean remove(Runnable task) { |
529 |
dl |
1.22 |
if (task instanceof ScheduledCancellable) |
530 |
|
|
return super.remove(task); |
531 |
dl |
1.15 |
|
532 |
tim |
1.21 |
// The task might actually have been wrapped as a ScheduledCancellable |
533 |
dl |
1.15 |
// in execute(), in which case we need to maually traverse |
534 |
|
|
// looking for it. |
535 |
|
|
|
536 |
tim |
1.21 |
ScheduledCancellable wrap = null; |
537 |
dl |
1.20 |
Object[] entries = super.getQueue().toArray(); |
538 |
dl |
1.15 |
for (int i = 0; i < entries.length; ++i) { |
539 |
tim |
1.21 |
ScheduledCancellableTask t = (ScheduledCancellableTask)entries[i]; |
540 |
dl |
1.15 |
Runnable r = t.getRunnable(); |
541 |
|
|
if (task.equals(r)) { |
542 |
|
|
wrap = t; |
543 |
|
|
break; |
544 |
|
|
} |
545 |
|
|
} |
546 |
|
|
entries = null; |
547 |
dl |
1.20 |
return wrap != null && super.getQueue().remove(wrap); |
548 |
|
|
} |
549 |
|
|
|
550 |
|
|
|
551 |
|
|
/** |
552 |
dl |
1.22 |
* Returns the task queue used by this executor. Each element of |
553 |
dl |
1.25 |
* this queue is a {@link ScheduledCancellable}, including those |
554 |
dl |
1.22 |
* tasks submitted using <tt>execute</tt> which are for scheduling |
555 |
|
|
* purposes used as the basis of a zero-delay |
556 |
|
|
* <tt>ScheduledCancellable</tt>. Iteration over this queue is |
557 |
|
|
* </em>not</em> guaranteed to travserse tasks in the order in |
558 |
|
|
* which they will execute. |
559 |
dl |
1.20 |
* |
560 |
|
|
* @return the task queue |
561 |
|
|
*/ |
562 |
|
|
public BlockingQueue<Runnable> getQueue() { |
563 |
|
|
return super.getQueue(); |
564 |
dl |
1.15 |
} |
565 |
|
|
|
566 |
|
|
/** |
567 |
dl |
1.22 |
* Override of <tt>Executor</tt> hook method to support periodic |
568 |
|
|
* tasks. If the executed task was periodic, causes the task for |
569 |
|
|
* the next period to execute. |
570 |
tim |
1.21 |
* @param r the task (assumed to be a ScheduledCancellable) |
571 |
dl |
1.9 |
* @param t the exception |
572 |
dl |
1.4 |
*/ |
573 |
|
|
protected void afterExecute(Runnable r, Throwable t) { |
574 |
|
|
super.afterExecute(r, t); |
575 |
tim |
1.21 |
ScheduledCancellableTask next = ((ScheduledCancellableTask)r).nextTask(); |
576 |
dl |
1.18 |
if (next != null && |
577 |
|
|
(!isShutdown() || |
578 |
|
|
(getContinueExistingPeriodicTasksAfterShutdownPolicy() && |
579 |
|
|
!isTerminating()))) |
580 |
dl |
1.20 |
super.getQueue().offer(next); |
581 |
dl |
1.18 |
|
582 |
|
|
// This might have been the final executed delayed task. Wake |
583 |
|
|
// up threads to check. |
584 |
|
|
else if (isShutdown()) |
585 |
|
|
interruptIdleWorkers(); |
586 |
dl |
1.4 |
} |
587 |
tim |
1.1 |
} |