ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ScheduledExecutorService.java
Revision: 1.30
Committed: Thu Mar 7 00:50:36 2019 UTC (5 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.29: +1 -1 lines
Log Message:
8220248: fix headings in java.util.concurrent

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
9 /**
10 * An {@link ExecutorService} that can schedule commands to run after a given
11 * delay, or to execute periodically.
12 *
13 * <p>The {@code schedule} methods create tasks with various delays
14 * and return a task object that can be used to cancel or check
15 * execution. The {@code scheduleAtFixedRate} and
16 * {@code scheduleWithFixedDelay} methods create and execute tasks
17 * that run periodically until cancelled.
18 *
19 * <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 * periods) are also allowed in {@code schedule} methods, and are
23 * treated as requests for immediate execution.
24 *
25 * <p>All {@code schedule} methods accept <em>relative</em> delays and
26 * 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 * a certain future {@code date}, you can use: {@code schedule(task,
30 * date.getTime() - System.currentTimeMillis(),
31 * TimeUnit.MILLISECONDS)}. Beware however that expiration of a
32 * relative delay need not coincide with the current {@code Date} at
33 * which the task is enabled due to network time synchronization
34 * protocols, clock drift, or other factors.
35 *
36 * <p>The {@link Executors} class provides convenient factory methods for
37 * the ScheduledExecutorService implementations provided in this package.
38 *
39 * <h2>Usage Example</h2>
40 *
41 * Here is a class with a method that sets up a ScheduledExecutorService
42 * to beep every ten seconds for an hour:
43 *
44 * <pre> {@code
45 * import static java.util.concurrent.TimeUnit.*;
46 * class BeeperControl {
47 * private final ScheduledExecutorService scheduler =
48 * Executors.newScheduledThreadPool(1);
49 *
50 * public void beepForAnHour() {
51 * Runnable beeper = () -> System.out.println("beep");
52 * ScheduledFuture<?> beeperHandle =
53 * scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
54 * Runnable canceller = () -> beeperHandle.cancel(false);
55 * scheduler.schedule(canceller, 1, HOURS);
56 * }
57 * }}</pre>
58 *
59 * @since 1.5
60 * @author Doug Lea
61 */
62 public interface ScheduledExecutorService extends ExecutorService {
63
64 /**
65 * Submits a one-shot task that becomes enabled after the given delay.
66 *
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 * the task and whose {@code get()} method will return
72 * {@code null} upon completion
73 * @throws RejectedExecutionException if the task cannot be
74 * scheduled for execution
75 * @throws NullPointerException if command or unit is null
76 */
77 public ScheduledFuture<?> schedule(Runnable command,
78 long delay, TimeUnit unit);
79
80 /**
81 * Submits a value-returning one-shot task that becomes enabled
82 * after the given delay.
83 *
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 * @param <V> the type of the callable's result
88 * @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 * @throws NullPointerException if callable or unit is null
92 */
93 public <V> ScheduledFuture<V> schedule(Callable<V> callable,
94 long delay, TimeUnit unit);
95
96 /**
97 * 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 * {@code initialDelay}, then {@code initialDelay + period}, then
101 * {@code initialDelay + 2 * period}, and so on.
102 *
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 * calling {@link Future#get() get} on the returned future will throw
111 * {@link ExecutionException}, holding the exception as its cause.
112 * </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 *
121 * @param command the task to execute
122 * @param initialDelay the time to delay first execution
123 * @param period the period between successive executions
124 * @param unit the time unit of the initialDelay and period parameters
125 * @return a ScheduledFuture representing pending completion of
126 * 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 * @throws RejectedExecutionException if the task cannot be
131 * scheduled for execution
132 * @throws NullPointerException if command or unit is null
133 * @throws IllegalArgumentException if period less than or equal to zero
134 */
135 public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
136 long initialDelay,
137 long period,
138 TimeUnit unit);
139
140 /**
141 * 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 *
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 * calling {@link Future#get() get} on the returned future will throw
154 * {@link ExecutionException}, holding the exception as its cause.
155 * </ul>
156 * Subsequent executions are suppressed. Subsequent calls to
157 * {@link Future#isDone isDone()} on the returned future will
158 * return {@code true}.
159 *
160 * @param command the task to execute
161 * @param initialDelay the time to delay first execution
162 * @param delay the delay between the termination of one
163 * execution and the commencement of the next
164 * @param unit the time unit of the initialDelay and delay parameters
165 * @return a ScheduledFuture representing pending completion of
166 * 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 * @throws RejectedExecutionException if the task cannot be
171 * scheduled for execution
172 * @throws NullPointerException if command or unit is null
173 * @throws IllegalArgumentException if delay less than or equal to zero
174 */
175 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
176 long initialDelay,
177 long delay,
178 TimeUnit unit);
179
180 }