J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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 |
| 6 | * published by the Free Software Foundation. Sun designates this |
| 7 | * particular file as subject to the "Classpath" exception as provided |
| 8 | * by Sun in the LICENSE file that accompanied this code. |
| 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 | * |
| 20 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 21 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 22 | * have any questions. |
| 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 |
| 33 | * http://creativecommons.org/licenses/publicdomain |
| 34 | */ |
| 35 | |
| 36 | package java.util.concurrent; |
| 37 | import java.util.concurrent.atomic.*; |
| 38 | import java.util.*; |
| 39 | |
| 40 | /** |
| 41 | * An {@link ExecutorService} that can schedule commands to run after a given |
| 42 | * delay, or to execute periodically. |
| 43 | * |
| 44 | * <p> The <tt>schedule</tt> methods create tasks with various delays |
| 45 | * and return a task object that can be used to cancel or check |
| 46 | * execution. The <tt>scheduleAtFixedRate</tt> and |
| 47 | * <tt>scheduleWithFixedDelay</tt> methods create and execute tasks |
| 48 | * that run periodically until cancelled. |
| 49 | * |
| 50 | * <p> Commands submitted using the {@link Executor#execute} and |
| 51 | * {@link ExecutorService} <tt>submit</tt> methods are scheduled with |
| 52 | * a requested delay of zero. Zero and negative delays (but not |
| 53 | * periods) are also allowed in <tt>schedule</tt> methods, and are |
| 54 | * treated as requests for immediate execution. |
| 55 | * |
| 56 | * <p>All <tt>schedule</tt> methods accept <em>relative</em> delays and |
| 57 | * periods as arguments, not absolute times or dates. It is a simple |
| 58 | * matter to transform an absolute time represented as a {@link |
| 59 | * java.util.Date} to the required form. For example, to schedule at |
| 60 | * a certain future <tt>date</tt>, you can use: <tt>schedule(task, |
| 61 | * date.getTime() - System.currentTimeMillis(), |
| 62 | * TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a |
| 63 | * relative delay need not coincide with the current <tt>Date</tt> at |
| 64 | * which the task is enabled due to network time synchronization |
| 65 | * protocols, clock drift, or other factors. |
| 66 | * |
| 67 | * The {@link Executors} class provides convenient factory methods for |
| 68 | * the ScheduledExecutorService implementations provided in this package. |
| 69 | * |
| 70 | * <h3>Usage Example</h3> |
| 71 | * |
| 72 | * Here is a class with a method that sets up a ScheduledExecutorService |
| 73 | * to beep every ten seconds for an hour: |
| 74 | * |
| 75 | * <pre> |
| 76 | * import static java.util.concurrent.TimeUnit.*; |
| 77 | * class BeeperControl { |
| 78 | * private final ScheduledExecutorService scheduler = |
| 79 | * Executors.newScheduledThreadPool(1); |
| 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> |
| 93 | * |
| 94 | * @since 1.5 |
| 95 | * @author Doug Lea |
| 96 | */ |
| 97 | public interface ScheduledExecutorService extends ExecutorService { |
| 98 | |
| 99 | /** |
| 100 | * Creates and executes a one-shot action that becomes enabled |
| 101 | * after the given delay. |
| 102 | * |
| 103 | * @param command the task to execute |
| 104 | * @param delay the time from now to delay execution |
| 105 | * @param unit the time unit of the delay parameter |
| 106 | * @return a ScheduledFuture representing pending completion of |
| 107 | * the task and whose <tt>get()</tt> method will return |
| 108 | * <tt>null</tt> upon completion |
| 109 | * @throws RejectedExecutionException if the task cannot be |
| 110 | * scheduled for execution |
| 111 | * @throws NullPointerException if command is null |
| 112 | */ |
| 113 | public ScheduledFuture<?> schedule(Runnable command, |
| 114 | long delay, TimeUnit unit); |
| 115 | |
| 116 | /** |
| 117 | * Creates and executes a ScheduledFuture that becomes enabled after the |
| 118 | * given delay. |
| 119 | * |
| 120 | * @param callable the function to execute |
| 121 | * @param delay the time from now to delay execution |
| 122 | * @param unit the time unit of the delay parameter |
| 123 | * @return a ScheduledFuture that can be used to extract result or cancel |
| 124 | * @throws RejectedExecutionException if the task cannot be |
| 125 | * scheduled for execution |
| 126 | * @throws NullPointerException if callable is null |
| 127 | */ |
| 128 | public <V> ScheduledFuture<V> schedule(Callable<V> callable, |
| 129 | long delay, TimeUnit unit); |
| 130 | |
| 131 | /** |
| 132 | * Creates and executes a periodic action that becomes enabled first |
| 133 | * after the given initial delay, and subsequently with the given |
| 134 | * period; that is executions will commence after |
| 135 | * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then |
| 136 | * <tt>initialDelay + 2 * period</tt>, and so on. |
| 137 | * If any execution of the task |
| 138 | * encounters an exception, subsequent executions are suppressed. |
| 139 | * Otherwise, the task will only terminate via cancellation or |
| 140 | * termination of the executor. If any execution of this task |
| 141 | * takes longer than its period, then subsequent executions |
| 142 | * may start late, but will not concurrently execute. |
| 143 | * |
| 144 | * @param command the task to execute |
| 145 | * @param initialDelay the time to delay first execution |
| 146 | * @param period the period between successive executions |
| 147 | * @param unit the time unit of the initialDelay and period parameters |
| 148 | * @return a ScheduledFuture representing pending completion of |
| 149 | * the task, and whose <tt>get()</tt> method will throw an |
| 150 | * exception upon cancellation |
| 151 | * @throws RejectedExecutionException if the task cannot be |
| 152 | * scheduled for execution |
| 153 | * @throws NullPointerException if command is null |
| 154 | * @throws IllegalArgumentException if period less than or equal to zero |
| 155 | */ |
| 156 | public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, |
| 157 | long initialDelay, |
| 158 | long period, |
| 159 | TimeUnit unit); |
| 160 | |
| 161 | /** |
| 162 | * Creates and executes a periodic action that becomes enabled first |
| 163 | * after the given initial delay, and subsequently with the |
| 164 | * given delay between the termination of one execution and the |
| 165 | * commencement of the next. If any execution of the task |
| 166 | * encounters an exception, subsequent executions are suppressed. |
| 167 | * Otherwise, the task will only terminate via cancellation or |
| 168 | * termination of the executor. |
| 169 | * |
| 170 | * @param command the task to execute |
| 171 | * @param initialDelay the time to delay first execution |
| 172 | * @param delay the delay between the termination of one |
| 173 | * execution and the commencement of the next |
| 174 | * @param unit the time unit of the initialDelay and delay parameters |
| 175 | * @return a ScheduledFuture representing pending completion of |
| 176 | * the task, and whose <tt>get()</tt> method will throw an |
| 177 | * exception upon cancellation |
| 178 | * @throws RejectedExecutionException if the task cannot be |
| 179 | * scheduled for execution |
| 180 | * @throws NullPointerException if command is null |
| 181 | * @throws IllegalArgumentException if delay less than or equal to zero |
| 182 | */ |
| 183 | public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, |
| 184 | long initialDelay, |
| 185 | long delay, |
| 186 | TimeUnit unit); |
| 187 | |
| 188 | } |