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