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/licenses/publicdomain |
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 <t>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 |
* @since 1.5 |
39 |
* @author Doug Lea |
40 |
*/ |
41 |
public interface ScheduledExecutorService extends ExecutorService { |
42 |
|
43 |
/** |
44 |
* Creates and executes a one-shot action that becomes enabled |
45 |
* after the given delay. |
46 |
* @param command the task to execute. |
47 |
* @param delay the time from now to delay execution. |
48 |
* @param unit the time unit of the delay parameter. |
49 |
* @return a Future representing pending completion of the task, |
50 |
* and whose <tt>get()</tt> method will return <tt>null</tt> |
51 |
* upon completion. |
52 |
* @throws RejectedExecutionException if task cannot be scheduled |
53 |
* for execution. |
54 |
* @throws NullPointerException if command is null |
55 |
*/ |
56 |
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); |
57 |
|
58 |
/** |
59 |
* Creates and executes a ScheduledFuture that becomes enabled after the |
60 |
* given delay. |
61 |
* @param callable the function to execute. |
62 |
* @param delay the time from now to delay execution. |
63 |
* @param unit the time unit of the delay parameter. |
64 |
* @return a ScheduledFuture that can be used to extract result or cancel. |
65 |
* @throws RejectedExecutionException if task cannot be scheduled |
66 |
* for execution. |
67 |
* @throws NullPointerException if callable is null |
68 |
*/ |
69 |
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit); |
70 |
|
71 |
/** |
72 |
* Creates and executes a periodic action that becomes enabled first |
73 |
* after the given initial delay, and subsequently with the given |
74 |
* period; that is executions will commence after |
75 |
* <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then |
76 |
* <tt>initialDelay + 2 * period</tt>, and so on. |
77 |
* If any execution of the task |
78 |
* encounters an exception, subsequent executions are suppressed. |
79 |
* Otherwise, the task will only terminate via cancellation or |
80 |
* termination of the executor. |
81 |
* @param command the task to execute. |
82 |
* @param initialDelay the time to delay first execution. |
83 |
* @param period the period between successive executions. |
84 |
* @param unit the time unit of the initialDelay and period parameters |
85 |
* @return a Future representing pending completion of the task, |
86 |
* and whose <tt>get()</tt> method will throw an exception upon |
87 |
* cancellation. |
88 |
* @throws RejectedExecutionException if task cannot be scheduled |
89 |
* for execution. |
90 |
* @throws NullPointerException if command is null |
91 |
* @throws IllegalArgumentException if period less than or equal to zero. |
92 |
*/ |
93 |
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); |
94 |
|
95 |
/** |
96 |
* Creates and executes a periodic action that becomes enabled first |
97 |
* after the given initial delay, and subsequently with the |
98 |
* given delay between the termination of one execution and the |
99 |
* commencement of the next. If any execution of the task |
100 |
* encounters an exception, subsequent executions are suppressed. |
101 |
* Otherwise, the task will only terminate via cancellation or |
102 |
* termination of the executor. |
103 |
* @param command the task to execute. |
104 |
* @param initialDelay the time to delay first execution. |
105 |
* @param delay the delay between the termination of one |
106 |
* execution and the commencement of the next. |
107 |
* @param unit the time unit of the initialDelay and delay parameters |
108 |
* @return a Future representing pending completion of the task, |
109 |
* and whose <tt>get()</tt> method will throw an exception upon |
110 |
* cancellation. |
111 |
* @throws RejectedExecutionException if task cannot be scheduled |
112 |
* for execution. |
113 |
* @throws NullPointerException if command is null |
114 |
* @throws IllegalArgumentException if delay less than or equal to zero. |
115 |
*/ |
116 |
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); |
117 |
|
118 |
} |