ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jdk7/java/util/concurrent/ScheduledExecutorService.java
Revision: 1.3
Committed: Sat Jul 20 20:15:46 2013 UTC (10 years, 10 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +5 -4 lines
Log Message:
sync javadoc fixes from src/main

File Contents

# User Rev Content
1 dl 1.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 jsr166 1.2 * <p>The {@code schedule} methods create tasks with various delays
14 dl 1.1 * and return a task object that can be used to cancel or check
15 jsr166 1.2 * execution. The {@code scheduleAtFixedRate} and
16     * {@code scheduleWithFixedDelay} methods create and execute tasks
17 dl 1.1 * that run periodically until cancelled.
18     *
19 jsr166 1.3 * <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 jsr166 1.2 * periods) are also allowed in {@code schedule} methods, and are
23 dl 1.1 * treated as requests for immediate execution.
24     *
25 jsr166 1.2 * <p>All {@code schedule} methods accept <em>relative</em> delays and
26 dl 1.1 * 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 jsr166 1.2 * a certain future {@code date}, you can use: {@code schedule(task,
30 dl 1.1 * date.getTime() - System.currentTimeMillis(),
31 jsr166 1.2 * TimeUnit.MILLISECONDS)}. Beware however that expiration of a
32     * relative delay need not coincide with the current {@code Date} at
33 dl 1.1 * which the task is enabled due to network time synchronization
34     * protocols, clock drift, or other factors.
35     *
36 jsr166 1.3 * <p>The {@link Executors} class provides convenient factory methods for
37 dl 1.1 * the ScheduledExecutorService implementations provided in this package.
38     *
39     * <h3>Usage Example</h3>
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     * final Runnable beeper = new Runnable() {
52     * public void run() { System.out.println("beep"); }
53     * };
54     * final ScheduledFuture<?> beeperHandle =
55     * scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
56     * scheduler.schedule(new Runnable() {
57     * public void run() { beeperHandle.cancel(true); }
58     * }, 60 * 60, SECONDS);
59     * }
60     * }}</pre>
61     *
62     * @since 1.5
63     * @author Doug Lea
64     */
65     public interface ScheduledExecutorService extends ExecutorService {
66    
67     /**
68     * Creates and executes a one-shot action that becomes enabled
69     * after the given delay.
70     *
71     * @param command the task to execute
72     * @param delay the time from now to delay execution
73     * @param unit the time unit of the delay parameter
74     * @return a ScheduledFuture representing pending completion of
75 jsr166 1.2 * the task and whose {@code get()} method will return
76     * {@code null} upon completion
77 dl 1.1 * @throws RejectedExecutionException if the task cannot be
78     * scheduled for execution
79     * @throws NullPointerException if command is null
80     */
81     public ScheduledFuture<?> schedule(Runnable command,
82     long delay, TimeUnit unit);
83    
84     /**
85     * Creates and executes a ScheduledFuture that becomes enabled after the
86     * given delay.
87     *
88     * @param callable the function to execute
89     * @param delay the time from now to delay execution
90     * @param unit the time unit of the delay parameter
91 jsr166 1.3 * @param <V> the type of the callable's result
92 dl 1.1 * @return a ScheduledFuture that can be used to extract result or cancel
93     * @throws RejectedExecutionException if the task cannot be
94     * scheduled for execution
95     * @throws NullPointerException if callable is null
96     */
97     public <V> ScheduledFuture<V> schedule(Callable<V> callable,
98     long delay, TimeUnit unit);
99    
100     /**
101     * Creates and executes a periodic action that becomes enabled first
102     * after the given initial delay, and subsequently with the given
103     * period; that is executions will commence after
104 jsr166 1.2 * {@code initialDelay} then {@code initialDelay+period}, then
105     * {@code initialDelay + 2 * period}, and so on.
106 dl 1.1 * If any execution of the task
107     * encounters an exception, subsequent executions are suppressed.
108     * Otherwise, the task will only terminate via cancellation or
109     * termination of the executor. If any execution of this task
110     * takes longer than its period, then subsequent executions
111     * may start late, but will not concurrently execute.
112     *
113     * @param command the task to execute
114     * @param initialDelay the time to delay first execution
115     * @param period the period between successive executions
116     * @param unit the time unit of the initialDelay and period parameters
117     * @return a ScheduledFuture representing pending completion of
118 jsr166 1.2 * the task, and whose {@code get()} method will throw an
119 dl 1.1 * exception upon cancellation
120     * @throws RejectedExecutionException if the task cannot be
121     * scheduled for execution
122     * @throws NullPointerException if command is null
123     * @throws IllegalArgumentException if period less than or equal to zero
124     */
125     public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
126     long initialDelay,
127     long period,
128     TimeUnit unit);
129    
130     /**
131     * Creates and executes a periodic action that becomes enabled first
132     * after the given initial delay, and subsequently with the
133     * given delay between the termination of one execution and the
134     * commencement of the next. If any execution of the task
135     * encounters an exception, subsequent executions are suppressed.
136     * Otherwise, the task will only terminate via cancellation or
137     * termination of the executor.
138     *
139     * @param command the task to execute
140     * @param initialDelay the time to delay first execution
141     * @param delay the delay between the termination of one
142     * execution and the commencement of the next
143     * @param unit the time unit of the initialDelay and delay parameters
144     * @return a ScheduledFuture representing pending completion of
145 jsr166 1.2 * the task, and whose {@code get()} method will throw an
146 dl 1.1 * exception upon cancellation
147     * @throws RejectedExecutionException if the task cannot be
148     * scheduled for execution
149     * @throws NullPointerException if command is null
150     * @throws IllegalArgumentException if delay less than or equal to zero
151     */
152     public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
153     long initialDelay,
154     long delay,
155     TimeUnit unit);
156    
157     }