28 |
|
* <tt>corePoolSize</tt> threads and an unbounded queue, adjustments |
29 |
|
* to <tt>maximumPoolSize</tt> have no useful effect. |
30 |
|
* |
31 |
+ |
* <p>This class supports protected extension method |
32 |
+ |
* <tt>decorateTask</tt> (one version each for <tt>Runnable</tt> and |
33 |
+ |
* <tt>Callable</tt>) that can be used to customize the concrete task |
34 |
+ |
* types used to execute commands. By default, |
35 |
+ |
* a <tt>ScheduledThreadPoolExecutor</tt> uses |
36 |
+ |
* a task type extending {@link FutureTask}. However, this may |
37 |
+ |
* be modified or replaced using subclasses of the form: |
38 |
+ |
* <pre> |
39 |
+ |
* public class CustomScheduledExecutor extends ScheduledThreadPoolExecutor { |
40 |
+ |
* |
41 |
+ |
* static class CustomTask<V> implements RunnableScheduledFuture<V> { ... } |
42 |
+ |
* |
43 |
+ |
* <V> protected RunnableScheduledFuture<V> decorateTask( |
44 |
+ |
* Runnable r, RunnableScheduledFuture<V> task) { |
45 |
+ |
* return new CustomTask<V>(r, task); |
46 |
+ |
* } |
47 |
+ |
* |
48 |
+ |
* <V> protected RunnableScheduledFuture<V> decorateTask( |
49 |
+ |
* Callable c, RunnableScheduledFuture<V> task) { |
50 |
+ |
* return new CustomTask<V>(c, task); |
51 |
+ |
* } |
52 |
+ |
* // ... add constructors, etc. |
53 |
+ |
* } |
54 |
+ |
* </pre> |
55 |
|
* @since 1.5 |
56 |
|
* @author Doug Lea |
57 |
|
*/ |
154 |
|
* Returns true if this is a periodic (not a one-shot) action. |
155 |
|
* @return true if periodic |
156 |
|
*/ |
157 |
< |
boolean isPeriodic() { |
157 |
> |
public boolean isPeriodic() { |
158 |
|
return period != 0; |
159 |
|
} |
160 |
|
|
222 |
|
Object[] entries = super.getQueue().toArray(); |
223 |
|
for (int i = 0; i < entries.length; ++i) { |
224 |
|
Object e = entries[i]; |
225 |
< |
if (e instanceof ScheduledFutureTask) { |
226 |
< |
ScheduledFutureTask<?> t = (ScheduledFutureTask<?>)e; |
225 |
> |
if (e instanceof RunnableScheduledFuture) { |
226 |
> |
RunnableScheduledFuture<?> t = (RunnableScheduledFuture<?>)e; |
227 |
|
if (t.isPeriodic()? !keepPeriodic : !keepDelayed) |
228 |
|
t.cancel(false); |
229 |
|
} |
234 |
|
} |
235 |
|
|
236 |
|
public boolean remove(Runnable task) { |
237 |
< |
if (!(task instanceof ScheduledFutureTask)) |
237 |
> |
if (!(task instanceof RunnableScheduledFuture)) |
238 |
|
return false; |
239 |
|
return getQueue().remove(task); |
240 |
|
} |
241 |
|
|
242 |
< |
|
242 |
> |
/** |
243 |
> |
* Modify or replace the task used to execute a runnable. |
244 |
> |
* This method can be used to override the concrete |
245 |
> |
* class used for managing internal tasks. |
246 |
> |
* The default implementation simply returns the given |
247 |
> |
* task. |
248 |
> |
* |
249 |
> |
* @param runnable the submitted Runnable |
250 |
> |
* @param task the task created to execute the runnable |
251 |
> |
* @return a task that can execute the runnable |
252 |
> |
* @since 1.6 |
253 |
> |
*/ |
254 |
|
protected <V> RunnableScheduledFuture<V> decorateTask( |
255 |
< |
Runnable runnable, RunnableScheduledFuture<V> f) { |
256 |
< |
return f; |
255 |
> |
Runnable runnable, RunnableScheduledFuture<V> task) { |
256 |
> |
return task; |
257 |
|
} |
258 |
|
|
259 |
+ |
/** |
260 |
+ |
* Modify or replace the task used to execute a callable. |
261 |
+ |
* This method can be used to override the concrete |
262 |
+ |
* class used for managing internal tasks. |
263 |
+ |
* The default implementation simply returns the given |
264 |
+ |
* task. |
265 |
+ |
* |
266 |
+ |
* @param callable the submitted Callable |
267 |
+ |
* @param task the task created to execute the callable |
268 |
+ |
* @return a task that can execute the callable |
269 |
+ |
* @since 1.6 |
270 |
+ |
*/ |
271 |
|
protected <V> RunnableScheduledFuture<V> decorateTask( |
272 |
< |
Callable<V> callable, RunnableScheduledFuture<V> f) { |
273 |
< |
return f; |
272 |
> |
Callable<V> callable, RunnableScheduledFuture<V> task) { |
273 |
> |
return task; |
274 |
|
} |
275 |
|
|
276 |
|
/** |
549 |
|
|
550 |
|
/** |
551 |
|
* An annoying wrapper class to convince generics compiler to |
552 |
< |
* use a DelayQueue<ScheduledFutureTask> as a BlockingQueue<Runnable> |
552 |
> |
* use a DelayQueue<RunnableScheduledFuture> as a BlockingQueue<Runnable> |
553 |
|
*/ |
554 |
|
private static class DelayedWorkQueue |
555 |
|
extends AbstractCollection<Runnable> |
556 |
|
implements BlockingQueue<Runnable> { |
557 |
|
|
558 |
< |
private final DelayQueue<ScheduledFutureTask> dq = new DelayQueue<ScheduledFutureTask>(); |
558 |
> |
private final DelayQueue<RunnableScheduledFuture> dq = new DelayQueue<RunnableScheduledFuture>(); |
559 |
|
public Runnable poll() { return dq.poll(); } |
560 |
|
public Runnable peek() { return dq.peek(); } |
561 |
|
public Runnable take() throws InterruptedException { return dq.take(); } |
563 |
|
return dq.poll(timeout, unit); |
564 |
|
} |
565 |
|
|
566 |
< |
public boolean add(Runnable x) { return dq.add((ScheduledFutureTask)x); } |
567 |
< |
public boolean offer(Runnable x) { return dq.offer((ScheduledFutureTask)x); } |
566 |
> |
public boolean add(Runnable x) { return dq.add((RunnableScheduledFuture)x); } |
567 |
> |
public boolean offer(Runnable x) { return dq.offer((RunnableScheduledFuture)x); } |
568 |
|
public void put(Runnable x) { |
569 |
< |
dq.put((ScheduledFutureTask)x); |
569 |
> |
dq.put((RunnableScheduledFuture)x); |
570 |
|
} |
571 |
|
public boolean offer(Runnable x, long timeout, TimeUnit unit) { |
572 |
< |
return dq.offer((ScheduledFutureTask)x, timeout, unit); |
572 |
> |
return dq.offer((RunnableScheduledFuture)x, timeout, unit); |
573 |
|
} |
574 |
|
|
575 |
|
public Runnable remove() { return dq.remove(); } |
589 |
|
public <T> T[] toArray(T[] array) { return dq.toArray(array); } |
590 |
|
public Iterator<Runnable> iterator() { |
591 |
|
return new Iterator<Runnable>() { |
592 |
< |
private Iterator<ScheduledFutureTask> it = dq.iterator(); |
592 |
> |
private Iterator<RunnableScheduledFuture> it = dq.iterator(); |
593 |
|
public boolean hasNext() { return it.hasNext(); } |
594 |
|
public Runnable next() { return it.next(); } |
595 |
|
public void remove() { it.remove(); } |