1 |
tim |
1.1 |
/* |
2 |
|
|
* @(#)ScheduledExecutor.java |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
package java.util.concurrent; |
6 |
|
|
|
7 |
|
|
/** |
8 |
|
|
* An <tt>Executor</tt> that can schedule tasks to run after a given delay, |
9 |
|
|
* or to execute periodically. This class is preferable to |
10 |
|
|
* <tt>java.util.Timer</tt> when multiple worker threads are needed, |
11 |
|
|
* or when the additional flexibility or capabilities of |
12 |
|
|
* <tt>ThreadExecutor</tt> are required. Tasks submitted using the |
13 |
|
|
* <tt>execute</tt> method are scheduled as if they had a requested |
14 |
|
|
* delay of zero. |
15 |
|
|
* |
16 |
|
|
* @since 1.5 |
17 |
|
|
* @see Executors |
18 |
|
|
* |
19 |
|
|
* @spec JSR-166 |
20 |
|
|
*/ |
21 |
|
|
public class ScheduledExecutor extends ThreadPoolExecutor { |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* 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 |
|
|
} |
41 |
|
|
|
42 |
|
|
/** |
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 { |
46 |
|
|
|
47 |
|
|
/** |
48 |
|
|
* 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() { |
63 |
|
|
} |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
/** |
67 |
|
|
* A delayed result-bearing action that can be run by a <tt>ScheduledExecutor</tt>. |
68 |
|
|
* |
69 |
|
|
* @see Future |
70 |
|
|
*/ |
71 |
|
|
public abstract class ScheduledFutureTask extends ScheduledTask implements Future { |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* Constructs scheduled future task. |
75 |
|
|
* @fixme default package access? |
76 |
|
|
*/ |
77 |
|
|
ScheduledFutureTask() { |
78 |
|
|
} |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
/** |
82 |
|
|
* Creates a delayed task that can be scheduled on this executor. |
83 |
|
|
* A delayed task becomes eligible to run after the specified delay expires. |
84 |
|
|
* |
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 |
|
|
} |
93 |
|
|
|
94 |
|
|
/** |
95 |
|
|
* Creates a periodic task. A periodic task is like a |
96 |
|
|
* delayed task, except that upon its completion, it is rescheduled |
97 |
|
|
* for repeated execution after the specified delay. Periodic tasks |
98 |
|
|
* will run no more than once every delay cycle, but are subject to |
99 |
|
|
* drift over time. |
100 |
|
|
* |
101 |
|
|
* @param r ??? |
102 |
|
|
* @param delay ??? |
103 |
|
|
* @param period ??? |
104 |
|
|
* @param unit ??? |
105 |
|
|
* @return a task that executes periodically |
106 |
|
|
*/ |
107 |
|
|
public static ScheduledTask newPeriodicTask(Runnable r, long delay, long period, TimeUnit unit) { |
108 |
|
|
return null; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
/** |
112 |
|
|
* Creates a fixed-rate task. A fixed rate task is like |
113 |
|
|
* a periodic task, except that upon completion, it is rescheduled |
114 |
|
|
* to run at the specified delay after the scheduled start time of |
115 |
|
|
* the current execution. ScheduledExecutor attempts to execute |
116 |
|
|
* fixed rate tasks at the desired frequency, even if the previous |
117 |
|
|
* execution was delayed. |
118 |
|
|
* |
119 |
|
|
* @param r ??? |
120 |
|
|
* @param delay ??? |
121 |
|
|
* @param period ??? |
122 |
|
|
* @param unit ??? |
123 |
|
|
* @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; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
/** |
130 |
|
|
* Creates a delayed Callable task, which computes a result. |
131 |
|
|
* |
132 |
|
|
* @param c ??? |
133 |
|
|
* @param delay ??? |
134 |
|
|
* @param unit ??? |
135 |
|
|
* @return a task that computes a result after the specified delay |
136 |
|
|
*/ |
137 |
|
|
public static ScheduledFutureTask newDelayedFutureTask(Callable c, long delay, TimeUnit unit) { |
138 |
|
|
return null; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
/** |
142 |
|
|
* Schedules a ScheduledTask for execution. |
143 |
|
|
* |
144 |
|
|
* @param t ??? |
145 |
|
|
* @throws CannotExecuteException if command cannot be scheduled for |
146 |
|
|
* execution |
147 |
|
|
*/ |
148 |
|
|
public void schedule(ScheduledTask t) { |
149 |
|
|
} |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
|
153 |
|
|
/* |
154 |
|
|
* @fixme static factories on internal or external classes? |
155 |
|
|
*/ |