1 |
tim |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.6 |
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.19 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
tim |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
|
|
|
9 |
|
|
/** |
10 |
tim |
1.2 |
* An {@link ExecutorService} that can schedule commands to run after a given |
11 |
jsr166 |
1.14 |
* delay, or to execute periodically. |
12 |
tim |
1.1 |
* |
13 |
jsr166 |
1.22 |
* <p>The {@code schedule} methods create tasks with various delays |
14 |
tim |
1.2 |
* and return a task object that can be used to cancel or check |
15 |
jsr166 |
1.22 |
* execution. The {@code scheduleAtFixedRate} and |
16 |
|
|
* {@code scheduleWithFixedDelay} methods create and execute tasks |
17 |
jsr166 |
1.14 |
* that run periodically until cancelled. |
18 |
dl |
1.4 |
* |
19 |
jsr166 |
1.23 |
* <p>Commands submitted using the {@link Executor#execute(Runnable)} |
20 |
|
|
* and {@link ExecutorService} {@code submit} methods are scheduled |
21 |
|
|
* with a requested delay of zero. Zero and negative delays (but not |
22 |
jsr166 |
1.22 |
* periods) are also allowed in {@code schedule} methods, and are |
23 |
dl |
1.4 |
* treated as requests for immediate execution. |
24 |
tim |
1.2 |
* |
25 |
jsr166 |
1.22 |
* <p>All {@code schedule} methods accept <em>relative</em> delays and |
26 |
tim |
1.2 |
* periods as arguments, not absolute times or dates. It is a simple |
27 |
|
|
* matter to transform an absolute time represented as a {@link |
28 |
|
|
* java.util.Date} to the required form. For example, to schedule at |
29 |
jsr166 |
1.22 |
* a certain future {@code date}, you can use: {@code schedule(task, |
30 |
tim |
1.2 |
* date.getTime() - System.currentTimeMillis(), |
31 |
jsr166 |
1.22 |
* TimeUnit.MILLISECONDS)}. Beware however that expiration of a |
32 |
|
|
* relative delay need not coincide with the current {@code Date} at |
33 |
tim |
1.2 |
* which the task is enabled due to network time synchronization |
34 |
jsr166 |
1.14 |
* protocols, clock drift, or other factors. |
35 |
tim |
1.1 |
* |
36 |
jsr166 |
1.24 |
* <p>The {@link Executors} class provides convenient factory methods for |
37 |
dl |
1.7 |
* the ScheduledExecutorService implementations provided in this package. |
38 |
|
|
* |
39 |
jsr166 |
1.30 |
* <h2>Usage Example</h2> |
40 |
jsr166 |
1.14 |
* |
41 |
dl |
1.8 |
* Here is a class with a method that sets up a ScheduledExecutorService |
42 |
|
|
* to beep every ten seconds for an hour: |
43 |
|
|
* |
44 |
jsr166 |
1.27 |
* <pre> {@code |
45 |
dl |
1.12 |
* import static java.util.concurrent.TimeUnit.*; |
46 |
dl |
1.8 |
* class BeeperControl { |
47 |
jsr166 |
1.18 |
* private final ScheduledExecutorService scheduler = |
48 |
|
|
* Executors.newScheduledThreadPool(1); |
49 |
dl |
1.8 |
* |
50 |
jsr166 |
1.18 |
* public void beepForAnHour() { |
51 |
jsr166 |
1.28 |
* Runnable beeper = () -> System.out.println("beep"); |
52 |
|
|
* ScheduledFuture<?> beeperHandle = |
53 |
jsr166 |
1.18 |
* scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); |
54 |
jsr166 |
1.29 |
* Runnable canceller = () -> beeperHandle.cancel(false); |
55 |
jsr166 |
1.28 |
* scheduler.schedule(canceller, 1, HOURS); |
56 |
jsr166 |
1.18 |
* } |
57 |
|
|
* }}</pre> |
58 |
dl |
1.8 |
* |
59 |
tim |
1.1 |
* @since 1.5 |
60 |
|
|
* @author Doug Lea |
61 |
|
|
*/ |
62 |
tim |
1.2 |
public interface ScheduledExecutorService extends ExecutorService { |
63 |
|
|
|
64 |
|
|
/** |
65 |
jsr166 |
1.29 |
* Submits a one-shot task that becomes enabled after the given delay. |
66 |
jsr166 |
1.16 |
* |
67 |
|
|
* @param command the task to execute |
68 |
|
|
* @param delay the time from now to delay execution |
69 |
|
|
* @param unit the time unit of the delay parameter |
70 |
|
|
* @return a ScheduledFuture representing pending completion of |
71 |
jsr166 |
1.22 |
* the task and whose {@code get()} method will return |
72 |
|
|
* {@code null} upon completion |
73 |
jsr166 |
1.16 |
* @throws RejectedExecutionException if the task cannot be |
74 |
|
|
* scheduled for execution |
75 |
jsr166 |
1.29 |
* @throws NullPointerException if command or unit is null |
76 |
tim |
1.2 |
*/ |
77 |
jsr166 |
1.16 |
public ScheduledFuture<?> schedule(Runnable command, |
78 |
jsr166 |
1.17 |
long delay, TimeUnit unit); |
79 |
tim |
1.2 |
|
80 |
|
|
/** |
81 |
jsr166 |
1.29 |
* Submits a value-returning one-shot task that becomes enabled |
82 |
|
|
* after the given delay. |
83 |
jsr166 |
1.16 |
* |
84 |
|
|
* @param callable the function to execute |
85 |
|
|
* @param delay the time from now to delay execution |
86 |
|
|
* @param unit the time unit of the delay parameter |
87 |
jsr166 |
1.25 |
* @param <V> the type of the callable's result |
88 |
jsr166 |
1.16 |
* @return a ScheduledFuture that can be used to extract result or cancel |
89 |
|
|
* @throws RejectedExecutionException if the task cannot be |
90 |
|
|
* scheduled for execution |
91 |
jsr166 |
1.29 |
* @throws NullPointerException if callable or unit is null |
92 |
tim |
1.2 |
*/ |
93 |
jsr166 |
1.16 |
public <V> ScheduledFuture<V> schedule(Callable<V> callable, |
94 |
jsr166 |
1.17 |
long delay, TimeUnit unit); |
95 |
tim |
1.2 |
|
96 |
|
|
/** |
97 |
jsr166 |
1.29 |
* Submits a periodic action that becomes enabled first after the |
98 |
|
|
* given initial delay, and subsequently with the given period; |
99 |
|
|
* that is, executions will commence after |
100 |
jsr166 |
1.26 |
* {@code initialDelay}, then {@code initialDelay + period}, then |
101 |
jsr166 |
1.22 |
* {@code initialDelay + 2 * period}, and so on. |
102 |
jsr166 |
1.26 |
* |
103 |
|
|
* <p>The sequence of task executions continues indefinitely until |
104 |
|
|
* one of the following exceptional completions occur: |
105 |
|
|
* <ul> |
106 |
|
|
* <li>The task is {@linkplain Future#cancel explicitly cancelled} |
107 |
|
|
* via the returned future. |
108 |
|
|
* <li>The executor terminates, also resulting in task cancellation. |
109 |
|
|
* <li>An execution of the task throws an exception. In this case |
110 |
jsr166 |
1.29 |
* calling {@link Future#get() get} on the returned future will throw |
111 |
|
|
* {@link ExecutionException}, holding the exception as its cause. |
112 |
jsr166 |
1.26 |
* </ul> |
113 |
|
|
* Subsequent executions are suppressed. Subsequent calls to |
114 |
|
|
* {@link Future#isDone isDone()} on the returned future will |
115 |
|
|
* return {@code true}. |
116 |
|
|
* |
117 |
|
|
* <p>If any execution of this task takes longer than its period, then |
118 |
|
|
* subsequent executions may start late, but will not concurrently |
119 |
|
|
* execute. |
120 |
jsr166 |
1.15 |
* |
121 |
jsr166 |
1.16 |
* @param command the task to execute |
122 |
|
|
* @param initialDelay the time to delay first execution |
123 |
|
|
* @param period the period between successive executions |
124 |
dl |
1.4 |
* @param unit the time unit of the initialDelay and period parameters |
125 |
jsr166 |
1.16 |
* @return a ScheduledFuture representing pending completion of |
126 |
jsr166 |
1.26 |
* the series of repeated tasks. The future's {@link |
127 |
|
|
* Future#get() get()} method will never return normally, |
128 |
|
|
* and will throw an exception upon task cancellation or |
129 |
|
|
* abnormal termination of a task execution. |
130 |
jsr166 |
1.16 |
* @throws RejectedExecutionException if the task cannot be |
131 |
|
|
* scheduled for execution |
132 |
jsr166 |
1.29 |
* @throws NullPointerException if command or unit is null |
133 |
jsr166 |
1.16 |
* @throws IllegalArgumentException if period less than or equal to zero |
134 |
tim |
1.2 |
*/ |
135 |
jsr166 |
1.15 |
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, |
136 |
jsr166 |
1.17 |
long initialDelay, |
137 |
|
|
long period, |
138 |
|
|
TimeUnit unit); |
139 |
jsr166 |
1.14 |
|
140 |
tim |
1.2 |
/** |
141 |
jsr166 |
1.29 |
* Submits a periodic action that becomes enabled first after the |
142 |
|
|
* given initial delay, and subsequently with the given delay |
143 |
|
|
* between the termination of one execution and the commencement of |
144 |
|
|
* the next. |
145 |
jsr166 |
1.26 |
* |
146 |
|
|
* <p>The sequence of task executions continues indefinitely until |
147 |
|
|
* one of the following exceptional completions occur: |
148 |
|
|
* <ul> |
149 |
|
|
* <li>The task is {@linkplain Future#cancel explicitly cancelled} |
150 |
|
|
* via the returned future. |
151 |
|
|
* <li>The executor terminates, also resulting in task cancellation. |
152 |
|
|
* <li>An execution of the task throws an exception. In this case |
153 |
jsr166 |
1.29 |
* calling {@link Future#get() get} on the returned future will throw |
154 |
|
|
* {@link ExecutionException}, holding the exception as its cause. |
155 |
jsr166 |
1.26 |
* </ul> |
156 |
|
|
* Subsequent executions are suppressed. Subsequent calls to |
157 |
|
|
* {@link Future#isDone isDone()} on the returned future will |
158 |
|
|
* return {@code true}. |
159 |
jsr166 |
1.15 |
* |
160 |
jsr166 |
1.16 |
* @param command the task to execute |
161 |
|
|
* @param initialDelay the time to delay first execution |
162 |
tim |
1.2 |
* @param delay the delay between the termination of one |
163 |
jsr166 |
1.16 |
* execution and the commencement of the next |
164 |
dl |
1.4 |
* @param unit the time unit of the initialDelay and delay parameters |
165 |
jsr166 |
1.16 |
* @return a ScheduledFuture representing pending completion of |
166 |
jsr166 |
1.26 |
* the series of repeated tasks. The future's {@link |
167 |
|
|
* Future#get() get()} method will never return normally, |
168 |
|
|
* and will throw an exception upon task cancellation or |
169 |
|
|
* abnormal termination of a task execution. |
170 |
jsr166 |
1.16 |
* @throws RejectedExecutionException if the task cannot be |
171 |
|
|
* scheduled for execution |
172 |
jsr166 |
1.29 |
* @throws NullPointerException if command or unit is null |
173 |
jsr166 |
1.16 |
* @throws IllegalArgumentException if delay less than or equal to zero |
174 |
tim |
1.2 |
*/ |
175 |
jsr166 |
1.15 |
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, |
176 |
jsr166 |
1.17 |
long initialDelay, |
177 |
|
|
long delay, |
178 |
|
|
TimeUnit unit); |
179 |
tim |
1.2 |
|
180 |
tim |
1.1 |
} |