Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/util/concurrent/ScheduledExecutorService.java
+++ new/src/share/classes/java/util/concurrent/ScheduledExecutorService.java
1 1 /*
2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 3 *
4 4 * This code is free software; you can redistribute it and/or modify it
5 5 * under the terms of the GNU General Public License version 2 only, as
6 6 * published by the Free Software Foundation. Oracle designates this
7 7 * particular file as subject to the "Classpath" exception as provided
8 8 * by Oracle in the LICENSE file that accompanied this code.
9 9 *
10 10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 13 * version 2 for more details (a copy is included in the LICENSE file that
14 14 * accompanied this code).
15 15 *
16 16 * You should have received a copy of the GNU General Public License version
17 17 * 2 along with this work; if not, write to the Free Software Foundation,
18 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 19 *
20 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 21 * or visit www.oracle.com if you need additional information or have any
22 22 * questions.
23 23 */
24 24
25 25 /*
26 26 * This file is available under and governed by the GNU General Public
27 27 * License version 2 only, as published by the Free Software Foundation.
28 28 * However, the following notice accompanied the original version of this
29 29 * file:
30 30 *
31 31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 32 * Expert Group and released to the public domain, as explained at
33 33 * http://creativecommons.org/licenses/publicdomain
34 34 */
35 35
36 36 package java.util.concurrent;
37 37 import java.util.concurrent.atomic.*;
38 38 import java.util.*;
39 39
40 40 /**
41 41 * An {@link ExecutorService} that can schedule commands to run after a given
42 42 * delay, or to execute periodically.
43 43 *
44 44 * <p> The <tt>schedule</tt> methods create tasks with various delays
45 45 * and return a task object that can be used to cancel or check
46 46 * execution. The <tt>scheduleAtFixedRate</tt> and
47 47 * <tt>scheduleWithFixedDelay</tt> methods create and execute tasks
48 48 * that run periodically until cancelled.
49 49 *
50 50 * <p> Commands submitted using the {@link Executor#execute} and
51 51 * {@link ExecutorService} <tt>submit</tt> methods are scheduled with
52 52 * a requested delay of zero. Zero and negative delays (but not
53 53 * periods) are also allowed in <tt>schedule</tt> methods, and are
54 54 * treated as requests for immediate execution.
55 55 *
56 56 * <p>All <tt>schedule</tt> methods accept <em>relative</em> delays and
57 57 * periods as arguments, not absolute times or dates. It is a simple
58 58 * matter to transform an absolute time represented as a {@link
59 59 * java.util.Date} to the required form. For example, to schedule at
60 60 * a certain future <tt>date</tt>, you can use: <tt>schedule(task,
61 61 * date.getTime() - System.currentTimeMillis(),
62 62 * TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a
63 63 * relative delay need not coincide with the current <tt>Date</tt> at
64 64 * which the task is enabled due to network time synchronization
↓ open down ↓ |
64 lines elided |
↑ open up ↑ |
65 65 * protocols, clock drift, or other factors.
66 66 *
67 67 * The {@link Executors} class provides convenient factory methods for
68 68 * the ScheduledExecutorService implementations provided in this package.
69 69 *
70 70 * <h3>Usage Example</h3>
71 71 *
72 72 * Here is a class with a method that sets up a ScheduledExecutorService
73 73 * to beep every ten seconds for an hour:
74 74 *
75 - * <pre>
75 + * <pre> {@code
76 76 * import static java.util.concurrent.TimeUnit.*;
77 77 * class BeeperControl {
78 - * private final ScheduledExecutorService scheduler =
79 - * Executors.newScheduledThreadPool(1);
78 + * private final ScheduledExecutorService scheduler =
79 + * Executors.newScheduledThreadPool(1);
80 80 *
81 - * public void beepForAnHour() {
82 - * final Runnable beeper = new Runnable() {
83 - * public void run() { System.out.println("beep"); }
84 - * };
85 - * final ScheduledFuture<?> beeperHandle =
86 - * scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
87 - * scheduler.schedule(new Runnable() {
88 - * public void run() { beeperHandle.cancel(true); }
89 - * }, 60 * 60, SECONDS);
90 - * }
91 - * }
92 - * </pre>
81 + * public void beepForAnHour() {
82 + * final Runnable beeper = new Runnable() {
83 + * public void run() { System.out.println("beep"); }
84 + * };
85 + * final ScheduledFuture<?> beeperHandle =
86 + * scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
87 + * scheduler.schedule(new Runnable() {
88 + * public void run() { beeperHandle.cancel(true); }
89 + * }, 60 * 60, SECONDS);
90 + * }
91 + * }}</pre>
93 92 *
94 93 * @since 1.5
95 94 * @author Doug Lea
96 95 */
97 96 public interface ScheduledExecutorService extends ExecutorService {
98 97
99 98 /**
100 99 * Creates and executes a one-shot action that becomes enabled
101 100 * after the given delay.
102 101 *
103 102 * @param command the task to execute
104 103 * @param delay the time from now to delay execution
105 104 * @param unit the time unit of the delay parameter
106 105 * @return a ScheduledFuture representing pending completion of
107 106 * the task and whose <tt>get()</tt> method will return
108 107 * <tt>null</tt> upon completion
109 108 * @throws RejectedExecutionException if the task cannot be
110 109 * scheduled for execution
111 110 * @throws NullPointerException if command is null
112 111 */
113 112 public ScheduledFuture<?> schedule(Runnable command,
114 113 long delay, TimeUnit unit);
115 114
116 115 /**
117 116 * Creates and executes a ScheduledFuture that becomes enabled after the
118 117 * given delay.
119 118 *
120 119 * @param callable the function to execute
121 120 * @param delay the time from now to delay execution
122 121 * @param unit the time unit of the delay parameter
123 122 * @return a ScheduledFuture that can be used to extract result or cancel
124 123 * @throws RejectedExecutionException if the task cannot be
125 124 * scheduled for execution
126 125 * @throws NullPointerException if callable is null
127 126 */
128 127 public <V> ScheduledFuture<V> schedule(Callable<V> callable,
129 128 long delay, TimeUnit unit);
130 129
131 130 /**
132 131 * Creates and executes a periodic action that becomes enabled first
133 132 * after the given initial delay, and subsequently with the given
134 133 * period; that is executions will commence after
135 134 * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
136 135 * <tt>initialDelay + 2 * period</tt>, and so on.
137 136 * If any execution of the task
138 137 * encounters an exception, subsequent executions are suppressed.
139 138 * Otherwise, the task will only terminate via cancellation or
140 139 * termination of the executor. If any execution of this task
141 140 * takes longer than its period, then subsequent executions
142 141 * may start late, but will not concurrently execute.
143 142 *
144 143 * @param command the task to execute
145 144 * @param initialDelay the time to delay first execution
146 145 * @param period the period between successive executions
147 146 * @param unit the time unit of the initialDelay and period parameters
148 147 * @return a ScheduledFuture representing pending completion of
149 148 * the task, and whose <tt>get()</tt> method will throw an
150 149 * exception upon cancellation
151 150 * @throws RejectedExecutionException if the task cannot be
152 151 * scheduled for execution
153 152 * @throws NullPointerException if command is null
154 153 * @throws IllegalArgumentException if period less than or equal to zero
155 154 */
156 155 public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
157 156 long initialDelay,
158 157 long period,
159 158 TimeUnit unit);
160 159
161 160 /**
162 161 * Creates and executes a periodic action that becomes enabled first
163 162 * after the given initial delay, and subsequently with the
164 163 * given delay between the termination of one execution and the
165 164 * commencement of the next. If any execution of the task
166 165 * encounters an exception, subsequent executions are suppressed.
167 166 * Otherwise, the task will only terminate via cancellation or
168 167 * termination of the executor.
169 168 *
170 169 * @param command the task to execute
171 170 * @param initialDelay the time to delay first execution
172 171 * @param delay the delay between the termination of one
173 172 * execution and the commencement of the next
174 173 * @param unit the time unit of the initialDelay and delay parameters
175 174 * @return a ScheduledFuture representing pending completion of
176 175 * the task, and whose <tt>get()</tt> method will throw an
177 176 * exception upon cancellation
178 177 * @throws RejectedExecutionException if the task cannot be
179 178 * scheduled for execution
180 179 * @throws NullPointerException if command is null
181 180 * @throws IllegalArgumentException if delay less than or equal to zero
182 181 */
183 182 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
184 183 long initialDelay,
185 184 long delay,
186 185 TimeUnit unit);
187 186
188 187 }
↓ open down ↓ |
86 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX