3 |
|
*/ |
4 |
|
|
5 |
|
package java.util.concurrent; |
6 |
+ |
import java.util.*; |
7 |
|
|
8 |
|
/** |
9 |
|
* An <tt>Executor</tt> that can schedule tasks to run after a given delay, |
21 |
|
*/ |
22 |
|
public class ScheduledExecutor extends ThreadPoolExecutor { |
23 |
|
|
24 |
< |
/** |
25 |
< |
* Creates a new ScheduledExecutor with the given initial parameters. |
25 |
< |
* |
26 |
< |
* @param minThreads the minimum number of threads to keep in the pool, |
27 |
< |
* even if they are idle |
28 |
< |
* @param maxThreads the maximum number of threads to allow in the pool |
29 |
< |
* @param keepAliveTime when the number of threads is greater than |
30 |
< |
* the minimum, this is the maximum time that excess idle threads |
31 |
< |
* will wait for new tasks before terminating |
32 |
< |
* @param granularity the time unit for the keepAliveTime argument |
33 |
< |
*/ |
34 |
< |
public ScheduledExecutor(int minThreads, |
35 |
< |
int maxThreads, |
36 |
< |
long keepAliveTime, |
37 |
< |
TimeUnit granularity) { |
38 |
< |
super(minThreads, maxThreads, keepAliveTime, granularity, |
39 |
< |
new PriorityBlockingQueue()); |
40 |
< |
} |
24 |
> |
private static class DelayedWorkQueue extends AbstractQueue<Runnable> implements BlockingQueue<Runnable> { |
25 |
> |
private DelayQueue<Runnable> dq = new DelayQueue<Runnable>(); |
26 |
|
|
27 |
< |
/** |
43 |
< |
* A delayed or periodic task that can be run by a <tt>ScheduledExecutor</tt>. |
44 |
< |
*/ |
45 |
< |
public abstract class ScheduledTask implements Runnable, Cancellable, Comparable { |
27 |
> |
DelayQueue<Runnable> getDelayQueue() { return dq; } |
28 |
|
|
29 |
< |
/** |
30 |
< |
* Returns the time interval until this task can run next, |
49 |
< |
* in the specified unit. |
50 |
< |
* |
51 |
< |
* @param unit time unit for interval returned |
52 |
< |
* @return time interval until task can run again |
53 |
< |
*/ |
54 |
< |
long getExecutionTime(TimeUnit unit) { |
55 |
< |
return 0; |
56 |
< |
} |
57 |
< |
|
58 |
< |
/** |
59 |
< |
* Constructs scheduled task. |
60 |
< |
* @fixme default package access? |
61 |
< |
*/ |
62 |
< |
ScheduledTask() { |
29 |
> |
public Runnable take() throws InterruptedException { |
30 |
> |
return dq.take().get(); |
31 |
|
} |
64 |
– |
} |
32 |
|
|
33 |
< |
/** |
34 |
< |
* A delayed result-bearing action that can be run by a <tt>ScheduledExecutor</tt>. |
35 |
< |
* |
36 |
< |
* @see Future |
70 |
< |
*/ |
71 |
< |
public abstract class ScheduledFutureTask extends ScheduledTask implements Future { |
33 |
> |
public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException { |
34 |
> |
DelayEntry<Runnable> e = dq.poll(timeout, unit); |
35 |
> |
return (e == null) ? null : e.get(); |
36 |
> |
} |
37 |
|
|
38 |
< |
/** |
39 |
< |
* Constructs scheduled future task. |
40 |
< |
* @fixme default package access? |
76 |
< |
*/ |
77 |
< |
ScheduledFutureTask() { |
38 |
> |
public Runnable poll() { |
39 |
> |
DelayEntry<Runnable> e = dq.poll(); |
40 |
> |
return (e == null) ? null : e.get(); |
41 |
|
} |
79 |
– |
} |
42 |
|
|
43 |
< |
/** |
44 |
< |
* Creates a delayed task that can be scheduled on this executor. |
45 |
< |
* A delayed task becomes eligible to run after the specified delay expires. |
46 |
< |
* |
85 |
< |
* @param r ??? |
86 |
< |
* @param delay ??? |
87 |
< |
* @param unit ??? |
88 |
< |
* @return a task that becomes eligible to run after the specified delay |
89 |
< |
*/ |
90 |
< |
public static ScheduledTask newDelayedTask(Runnable r, long delay, TimeUnit unit) { |
91 |
< |
return null; |
92 |
< |
} |
43 |
> |
public Runnable peek() { |
44 |
> |
DelayEntry<Runnable> e = dq.peek(); |
45 |
> |
return (e == null) ? null : e.get(); |
46 |
> |
} |
47 |
|
|
48 |
< |
/** |
49 |
< |
* Creates a periodic task. A periodic task is like a |
50 |
< |
* delayed task, except that upon its completion, it is rescheduled |
51 |
< |
* for repeated execution after the specified delay. Periodic tasks |
52 |
< |
* will run no more than once every delay cycle, but are subject to |
53 |
< |
* drift over time. |
54 |
< |
* |
55 |
< |
* @param r ??? |
56 |
< |
* @param delay ??? |
57 |
< |
* @param period ??? |
58 |
< |
* @param unit ??? |
59 |
< |
* @return a task that executes periodically |
60 |
< |
*/ |
61 |
< |
public static ScheduledTask newPeriodicTask(Runnable r, long delay, long period, TimeUnit unit) { |
62 |
< |
return null; |
63 |
< |
} |
48 |
> |
public int size() { return dq.size(); } |
49 |
> |
public int remainingCapacity() { return dq.remainingCapacity(); } |
50 |
> |
public void put(Runnable r) { |
51 |
> |
assert false; |
52 |
> |
} |
53 |
> |
public boolean offer(Runnable r) { |
54 |
> |
assert false; |
55 |
> |
return false; |
56 |
> |
} |
57 |
> |
public boolean offer(Runnable r, long time, TimeUnit unit) { |
58 |
> |
assert false; |
59 |
> |
return false; |
60 |
> |
} |
61 |
> |
public Iterator<Runnable> iterator() { |
62 |
> |
assert false; |
63 |
> |
return null; // for now |
64 |
> |
} |
65 |
|
|
66 |
< |
/** |
67 |
< |
* Creates a fixed-rate task. A fixed rate task is like |
68 |
< |
* a periodic task, except that upon completion, it is rescheduled |
69 |
< |
* to run at the specified delay after the scheduled start time of |
70 |
< |
* the current execution. ScheduledExecutor attempts to execute |
71 |
< |
* fixed rate tasks at the desired frequency, even if the previous |
72 |
< |
* execution was delayed. |
73 |
< |
* |
74 |
< |
* @param r ??? |
75 |
< |
* @param delay ??? |
76 |
< |
* @param period ??? |
77 |
< |
* @param unit ??? |
78 |
< |
* @return a task that executes periodically at a fixed rate |
124 |
< |
*/ |
125 |
< |
public static ScheduledTask newFixedRateTask(Runnable r, long delay, long period, TimeUnit unit) { |
126 |
< |
return null; |
66 |
> |
public boolean remove(Object x) { |
67 |
> |
assert false; |
68 |
> |
return false; // for now |
69 |
> |
} |
70 |
> |
|
71 |
> |
public Object[] toArray() { |
72 |
> |
assert false; |
73 |
> |
return null; // for now |
74 |
> |
} |
75 |
> |
public <T> T[] toArray(T[] a) { |
76 |
> |
assert false; |
77 |
> |
return null; // for now |
78 |
> |
} |
79 |
|
} |
80 |
|
|
81 |
+ |
|
82 |
|
/** |
83 |
< |
* Creates a delayed Callable task, which computes a result. |
84 |
< |
* |
85 |
< |
* @param c ??? |
86 |
< |
* @param delay ??? |
134 |
< |
* @param unit ??? |
135 |
< |
* @return a task that computes a result after the specified delay |
83 |
> |
* Creates a new ScheduledExecutor with the given initial parameters. |
84 |
> |
* |
85 |
> |
* @param corePoolSize the number of threads to keep in the pool, |
86 |
> |
* even if they are idle. |
87 |
|
*/ |
88 |
< |
public static ScheduledFutureTask newDelayedFutureTask(Callable c, long delay, TimeUnit unit) { |
89 |
< |
return null; |
88 |
> |
public ScheduledExecutor(int corePoolSize) { |
89 |
> |
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, |
90 |
> |
new DelayedWorkQueue()); |
91 |
|
} |
92 |
|
|
93 |
< |
/** |
94 |
< |
* Schedules a ScheduledTask for execution. |
95 |
< |
* |
96 |
< |
* @param t ??? |
97 |
< |
* @throws CannotExecuteException if command cannot be scheduled for |
98 |
< |
* execution |
99 |
< |
*/ |
100 |
< |
public void schedule(ScheduledTask t) { |
93 |
> |
public void execute(DelayEntry<Runnable> r) { |
94 |
> |
if (isShutdown()) { |
95 |
> |
getRejectedExecutionHandler().rejectedExecution(r.get(), this); |
96 |
> |
return; |
97 |
> |
} |
98 |
> |
|
99 |
> |
addIfUnderCorePoolSize(null); |
100 |
> |
((DelayedWorkQueue)getQueue()).getDelayQueue().put(r); |
101 |
|
} |
150 |
– |
} |
102 |
|
|
103 |
+ |
public void execute(Runnable r) { |
104 |
+ |
execute(new DelayEntry(r, 0, TimeUnit.NANOSECONDS)); |
105 |
+ |
} |
106 |
|
|
107 |
< |
/* |
154 |
< |
* @fixme static factories on internal or external classes? |
155 |
< |
*/ |
107 |
> |
} |