ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ScheduledExecutorService.java
Revision: 1.19
Committed: Tue Mar 15 19:47:03 2011 UTC (13 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.18: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 package java.util.concurrent;
8 import java.util.concurrent.atomic.*;
9 import java.util.*;
10
11 /**
12 * An {@link ExecutorService} that can schedule commands to run after a given
13 * delay, or to execute periodically.
14 *
15 * <p> The <tt>schedule</tt> methods create tasks with various delays
16 * and return a task object that can be used to cancel or check
17 * execution. The <tt>scheduleAtFixedRate</tt> and
18 * <tt>scheduleWithFixedDelay</tt> methods create and execute tasks
19 * that run periodically until cancelled.
20 *
21 * <p> Commands submitted using the {@link Executor#execute} and
22 * {@link ExecutorService} <tt>submit</tt> methods are scheduled with
23 * a requested delay of zero. Zero and negative delays (but not
24 * periods) are also allowed in <tt>schedule</tt> methods, and are
25 * treated as requests for immediate execution.
26 *
27 * <p>All <tt>schedule</tt> methods accept <em>relative</em> delays and
28 * periods as arguments, not absolute times or dates. It is a simple
29 * matter to transform an absolute time represented as a {@link
30 * java.util.Date} to the required form. For example, to schedule at
31 * a certain future <tt>date</tt>, you can use: <tt>schedule(task,
32 * date.getTime() - System.currentTimeMillis(),
33 * TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a
34 * relative delay need not coincide with the current <tt>Date</tt> at
35 * which the task is enabled due to network time synchronization
36 * protocols, clock drift, or other factors.
37 *
38 * The {@link Executors} class provides convenient factory methods for
39 * the ScheduledExecutorService implementations provided in this package.
40 *
41 * <h3>Usage Example</h3>
42 *
43 * Here is a class with a method that sets up a ScheduledExecutorService
44 * to beep every ten seconds for an hour:
45 *
46 * <pre> {@code
47 * import static java.util.concurrent.TimeUnit.*;
48 * class BeeperControl {
49 * private final ScheduledExecutorService scheduler =
50 * Executors.newScheduledThreadPool(1);
51 *
52 * public void beepForAnHour() {
53 * final Runnable beeper = new Runnable() {
54 * public void run() { System.out.println("beep"); }
55 * };
56 * final ScheduledFuture<?> beeperHandle =
57 * scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
58 * scheduler.schedule(new Runnable() {
59 * public void run() { beeperHandle.cancel(true); }
60 * }, 60 * 60, SECONDS);
61 * }
62 * }}</pre>
63 *
64 * @since 1.5
65 * @author Doug Lea
66 */
67 public interface ScheduledExecutorService extends ExecutorService {
68
69 /**
70 * Creates and executes a one-shot action that becomes enabled
71 * after the given delay.
72 *
73 * @param command the task to execute
74 * @param delay the time from now to delay execution
75 * @param unit the time unit of the delay parameter
76 * @return a ScheduledFuture representing pending completion of
77 * the task and whose <tt>get()</tt> method will return
78 * <tt>null</tt> upon completion
79 * @throws RejectedExecutionException if the task cannot be
80 * scheduled for execution
81 * @throws NullPointerException if command is null
82 */
83 public ScheduledFuture<?> schedule(Runnable command,
84 long delay, TimeUnit unit);
85
86 /**
87 * Creates and executes a ScheduledFuture that becomes enabled after the
88 * given delay.
89 *
90 * @param callable the function to execute
91 * @param delay the time from now to delay execution
92 * @param unit the time unit of the delay parameter
93 * @return a ScheduledFuture that can be used to extract result or cancel
94 * @throws RejectedExecutionException if the task cannot be
95 * scheduled for execution
96 * @throws NullPointerException if callable is null
97 */
98 public <V> ScheduledFuture<V> schedule(Callable<V> callable,
99 long delay, TimeUnit unit);
100
101 /**
102 * Creates and executes a periodic action that becomes enabled first
103 * after the given initial delay, and subsequently with the given
104 * period; that is executions will commence after
105 * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
106 * <tt>initialDelay + 2 * period</tt>, and so on.
107 * If any execution of the task
108 * encounters an exception, subsequent executions are suppressed.
109 * Otherwise, the task will only terminate via cancellation or
110 * termination of the executor. If any execution of this task
111 * takes longer than its period, then subsequent executions
112 * may start late, but will not concurrently execute.
113 *
114 * @param command the task to execute
115 * @param initialDelay the time to delay first execution
116 * @param period the period between successive executions
117 * @param unit the time unit of the initialDelay and period parameters
118 * @return a ScheduledFuture representing pending completion of
119 * the task, and whose <tt>get()</tt> method will throw an
120 * exception upon cancellation
121 * @throws RejectedExecutionException if the task cannot be
122 * scheduled for execution
123 * @throws NullPointerException if command is null
124 * @throws IllegalArgumentException if period less than or equal to zero
125 */
126 public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
127 long initialDelay,
128 long period,
129 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
134 * given delay between the termination of one execution and the
135 * commencement of the next. 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.
139 *
140 * @param command the task to execute
141 * @param initialDelay the time to delay first execution
142 * @param delay the delay between the termination of one
143 * execution and the commencement of the next
144 * @param unit the time unit of the initialDelay and delay parameters
145 * @return a ScheduledFuture representing pending completion of
146 * the task, and whose <tt>get()</tt> method will throw an
147 * exception upon cancellation
148 * @throws RejectedExecutionException if the task cannot be
149 * scheduled for execution
150 * @throws NullPointerException if command is null
151 * @throws IllegalArgumentException if delay less than or equal to zero
152 */
153 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
154 long initialDelay,
155 long delay,
156 TimeUnit unit);
157
158 }