1 *java.util.concurrent.ScheduledExecutorService* *ScheduledExecutorService* AnExe
3 public interface interface ScheduledExecutorService
5 implements |java.util.concurrent.ExecutorService|
7 |java.util.concurrent.ScheduledExecutorService_Description|
8 |java.util.concurrent.ScheduledExecutorService_Fields|
9 |java.util.concurrent.ScheduledExecutorService_Constructors|
10 |java.util.concurrent.ScheduledExecutorService_Methods|
12 ================================================================================
14 *java.util.concurrent.ScheduledExecutorService_Methods*
15 |java.util.concurrent.ScheduledExecutorService.schedule(Callable,long,TimeUnit)|
16 |java.util.concurrent.ScheduledExecutorService.schedule(Runnable,long,TimeUnit)|
17 |java.util.concurrent.ScheduledExecutorService.scheduleAtFixedRate(Runnable,long,long,TimeUnit)|
18 |java.util.concurrent.ScheduledExecutorService.scheduleWithFixedDelay(Runnable,long,long,TimeUnit)|
20 *java.util.concurrent.ScheduledExecutorService_Description*
22 An (|java.util.concurrent.ExecutorService|) that can schedule commands to run
23 after a given delay, or to execute periodically.
25 The schedule methods create tasks with various delays and return a task object
26 that can be used to cancel or check execution. The scheduleAtFixedRate and
27 scheduleWithFixedDelay methods create and execute tasks that run periodically
30 Commands submitted using the (|java.util.concurrent.Executor|) and
31 (|java.util.concurrent.ExecutorService|) submit methods are scheduled with a
32 requested delay of zero. Zero and negative delays (but not periods) are also
33 allowed in schedule methods, and are treated as requests for immediate
36 All schedule methods accept relative delays and periods as arguments, not
37 absolute times or dates. It is a simple matter to transform an absolute time
38 represented as a (|java.util.Date|) to the required form. For example, to
39 schedule at a certain future date, you can use: schedule(task, date.getTime() -
40 System.currentTimeMillis(), TimeUnit.MILLISECONDS). Beware however that
41 expiration of a relative delay need not coincide with the current Date at which
42 the task is enabled due to network time synchronization protocols, clock drift,
45 The (|java.util.concurrent.Executors|) class provides convenient factory
46 methods for the ScheduledExecutorService implementations provided in this
51 Here is a class with a method that sets up a ScheduledExecutorService to beep
52 every ten seconds for an hour:
56 import static java.util.concurrent.TimeUnit.*; class BeeperControl { private
57 final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
59 public void beepForAnHour() { final Runnable beeper = new Runnable() { public
60 void run() { System.out.println("beep"); } }; final ScheduledFuture<?>
61 beeperHandle = scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
62 scheduler.schedule(new Runnable() { public void run() {
63 beeperHandle.cancel(true); } }, 60 * 60, SECONDS); } }
66 *java.util.concurrent.ScheduledExecutorService.schedule(Callable,long,TimeUnit)*
68 public |java.util.concurrent.ScheduledFuture| schedule(
69 java.util.concurrent.Callable callable,
71 java.util.concurrent.TimeUnit unit)
73 Creates and executes a ScheduledFuture that becomes enabled after the given
76 callable - the function to execute.
77 delay - the time from now to delay execution.
78 unit - the time unit of the delay parameter.
80 Returns: a ScheduledFuture that can be used to extract result or cancel.
81 *java.util.concurrent.ScheduledExecutorService.schedule(Runnable,long,TimeUnit)*
83 public |java.util.concurrent.ScheduledFuture| schedule(
84 java.lang.Runnable command,
86 java.util.concurrent.TimeUnit unit)
88 Creates and executes a one-shot action that becomes enabled after the given
91 command - the task to execute.
92 delay - the time from now to delay execution.
93 unit - the time unit of the delay parameter.
95 Returns: a Future representing pending completion of the task, and whose get() method
96 will return null upon completion.
97 *java.util.concurrent.ScheduledExecutorService.scheduleAtFixedRate(Runnable,long,long,TimeUnit)*
99 public |java.util.concurrent.ScheduledFuture| scheduleAtFixedRate(
100 java.lang.Runnable command,
103 java.util.concurrent.TimeUnit unit)
105 Creates and executes a periodic action that becomes enabled first after the
106 given initial delay, and subsequently with the given period; that is executions
107 will commence after initialDelay then initialDelay+period, then initialDelay +
108 2 * period, and so on. If any execution of the task encounters an exception,
109 subsequent executions are suppressed. Otherwise, the task will only terminate
110 via cancellation or termination of the executor.
112 command - the task to execute.
113 initialDelay - the time to delay first execution.
114 period - the period between successive executions.
115 unit - the time unit of the initialDelay and period parameters
117 Returns: a Future representing pending completion of the task, and whose get() method
118 will throw an exception upon cancellation.
119 *java.util.concurrent.ScheduledExecutorService.scheduleWithFixedDelay(Runnable,long,long,TimeUnit)*
121 public |java.util.concurrent.ScheduledFuture| scheduleWithFixedDelay(
122 java.lang.Runnable command,
125 java.util.concurrent.TimeUnit unit)
127 Creates and executes a periodic action that becomes enabled first after the
128 given initial delay, and subsequently with the given delay between the
129 termination of one execution and the commencement of the next. If any execution
130 of the task encounters an exception, subsequent executions are suppressed.
131 Otherwise, the task will only terminate via cancellation or termination of the
134 command - the task to execute.
135 initialDelay - the time to delay first execution.
136 delay - the delay between the termination of one execution and the commencement of the
138 unit - the time unit of the initialDelay and delay parameters
140 Returns: a Future representing pending completion of the task, and whose get() method
141 will throw an exception upon cancellation.