1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain. Use, modify, and |
4 |
* redistribute this code in any way without acknowledgement. |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
import java.util.concurrent.atomic.*; |
9 |
import java.util.*; |
10 |
|
11 |
/** |
12 |
* An {@link Executor} 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. Commands submitted using |
20 |
* the <tt>execute</tt> method are scheduled with a requested delay of |
21 |
* zero. |
22 |
* |
23 |
* <p>All <t>schedule</tt> methods accept <em>relative</em> delays and |
24 |
* periods as arguments, not absolute times or dates. It is a simple |
25 |
* matter to transform an absolute time represented as a {@link |
26 |
* java.util.Date} to the required form. For example, to schedule at |
27 |
* a certain future <tt>date</tt>, you can use: <tt>schedule(task, |
28 |
* date.getTime() - System.currentTimeMillis(), |
29 |
* TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a |
30 |
* relative delay need not coincide with the current <tt>Date</tt> at |
31 |
* which the task is enabled due to network time synchronization |
32 |
* protocols, clock drift, or other factors. Negative relative delays |
33 |
* (but not periods) are allowed in <tt>schedule</tt> methods, and are |
34 |
* treated as requests for immediate execution. |
35 |
* |
36 |
* @since 1.5 |
37 |
* @author Doug Lea |
38 |
*/ |
39 |
public interface ScheduledExecutor extends Executor { |
40 |
|
41 |
/** |
42 |
* Creates and executes a one-shot action that becomes enabled |
43 |
* after the given delay. |
44 |
* @param command the task to execute. |
45 |
* @param delay the time from now to delay execution. |
46 |
* @param unit the time unit of the delay parameter. |
47 |
* @return a Future representing pending completion of the task, |
48 |
* and whose <tt>get()</tt> method will return an arbitrary value |
49 |
* upon completion. |
50 |
* @throws RejectedExecutionException if task cannot be scheduled |
51 |
* for execution because the executor has been shut down. |
52 |
* @throws NullPointerException if command is null |
53 |
*/ |
54 |
|
55 |
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); |
56 |
|
57 |
/** |
58 |
* Creates and executes a ScheduledFuture that becomes enabled after the |
59 |
* given delay. |
60 |
* @param callable the function to execute. |
61 |
* @param delay the time from now to delay execution. |
62 |
* @param unit the time unit of the delay parameter. |
63 |
* @return a ScheduledFuture that can be used to extract result or cancel. |
64 |
* @throws RejectedExecutionException if task cannot be scheduled |
65 |
* for execution because the executor has been shut down. |
66 |
* @throws NullPointerException if callable is null |
67 |
*/ |
68 |
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit); |
69 |
|
70 |
/** |
71 |
* Creates and executes a periodic action that becomes enabled first |
72 |
* after the given initial delay, and subsequently with the given |
73 |
* period; that is executions will commence after |
74 |
* <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then |
75 |
* <tt>initialDelay + 2 * period</tt>, and so on. The |
76 |
* task will only terminate via cancellation. |
77 |
* @param command the task to execute. |
78 |
* @param initialDelay the time to delay first execution. |
79 |
* @param period the period between successive executions. |
80 |
* @param unit the time unit of the delay and period parameters |
81 |
* @return a Future representing pending completion of the task, |
82 |
* and whose <tt>get()</tt> method will throw an exception upon |
83 |
* cancellation. |
84 |
* @throws RejectedExecutionException if task cannot be scheduled |
85 |
* for execution because the executor has been shut down. |
86 |
* @throws NullPointerException if command is null |
87 |
* @throws IllegalArgumentException if period less than or equal to zero. |
88 |
*/ |
89 |
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); |
90 |
/** |
91 |
* Creates and executes a periodic action that becomes enabled first |
92 |
* after the given initial delay, and subsequently with the |
93 |
* given delay between the termination of one execution and the |
94 |
* commencement of the next. |
95 |
* The task will only terminate via cancellation. |
96 |
* @param command the task to execute. |
97 |
* @param initialDelay the time to delay first execution. |
98 |
* @param delay the delay between the termination of one |
99 |
* execution and the commencement of the next. |
100 |
* @param unit the time unit of the delay and delay parameters |
101 |
* @return a Future representing pending completion of the task, |
102 |
* and whose <tt>get()</tt> method will throw an exception upon |
103 |
* cancellation. |
104 |
* @throws RejectedExecutionException if task cannot be scheduled |
105 |
* for execution because the executor has been shut down. |
106 |
* @throws NullPointerException if command is null |
107 |
* @throws IllegalArgumentException if delay less than or equal to zero. |
108 |
*/ |
109 |
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); |
110 |
|
111 |
/** |
112 |
* Execute command with zero required delay. This has effect |
113 |
* equivalent to <tt>schedule(command, 0, anyUnit)</tt>. |
114 |
* @param command the task to execute |
115 |
* @throws RejectedExecutionException if this task cannot be |
116 |
* accepted for execution. |
117 |
* @throws NullPointerException if command is null |
118 |
*/ |
119 |
public void execute(Runnable command); |
120 |
} |