public interface ScheduledExecutorService extends ExecutorService
ExecutorService
可以调度命令后运行一个给定的延迟,或定期执行。
的schedule
方法创造不同的延迟任务和返回一个对象,可以用来取消或检查执行。的scheduleAtFixedRate
和scheduleWithFixedDelay
方法创建和执行,定期运行直至取消任务。
命令提交使用Executor.execute(Runnable)
和ExecutorService
submit
方法是安排一个请求的延迟为零。零和负延迟(而不是时间)也在schedule
方法允许,并视为立即执行的请求。
所有的schedule
方法接受相对延迟和时间作为参数,而不是绝对的时间或日期。它是将绝对时间表示为一个Date
需要形成一个简单的事。例如,计划在将来的某个date
,你可以使用:schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)
。但是要小心,一个相对延迟到期不符合当前的任务是使Date
由于网络时间同步协议的时钟漂移,或其他因素。
的Executors
类提供了这个包提供ScheduledExecutorService实现方便工厂方法。
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
哔哔声
Modifier and Type | Method and Description |
---|---|
<V> ScheduledFuture<V> |
schedule(Callable<V> callable, long delay, TimeUnit unit)
创建和执行一个scheduledfuture成为给定的延迟后启用。
|
ScheduledFuture<?> |
schedule(Runnable command, long delay, TimeUnit unit)
创建并执行在给定的延迟后启用的一一个射击动作。
|
ScheduledFuture<?> |
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
创建和执行一个周期的动作变成了使后先给定的初始延迟,随后与给定的时期;这是执行将开始
initialDelay 然后
initialDelay+period ,然后
initialDelay + 2 * period ,等等。
|
ScheduledFuture<?> |
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
创建和执行一个周期性的行动,使第一次后,给定的初始延迟,并随后与给定的一个执行之间的终止和下一个开始的开始。
|
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow, submit, submit, submit
ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
command
-任务执行
delay
-时间从现在到延迟执行
unit
-延迟参数的时间单位
get()
方法将返回
null
完成后
RejectedExecutionException
如果任务不能按计划执行
NullPointerException
如果命令是无效的
<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
V
-可调用的结果的类型
callable
-函数的执行
delay
-时间从现在到延迟执行
unit
-延迟参数的时间单位
RejectedExecutionException
如果任务不能按计划执行
NullPointerException
如果赎回是空的
ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
initialDelay
然后
initialDelay+period
,然后
initialDelay + 2 * period
,等等。如果任务的任何执行遇到异常,则抑制后续的执行。否则,任务只会通过执行器的取消或终止而终止。如果这个任务的执行时间比它的周期长,那么随后的处决可能会开始晚,但不会同时执行。
command
-任务执行
initialDelay
-时间延迟第一次执行
period
之间连续执行期
unit
-时间单位的initialdelay和周期参数
get()
方法将抛出一个异常后取消
RejectedExecutionException
如果任务不能按计划执行
NullPointerException
如果命令是无效的
IllegalArgumentException
-如果时间小于或等于零
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
command
-任务执行
initialDelay
-时间延迟第一次执行
delay
-一个执行和下一个开始的结束之间的延迟
unit
的initialdelay和延迟参数的时间单位
get()
方法将抛出一个异常后取消
RejectedExecutionException
如果任务不能按计划执行
NullPointerException
如果命令是无效的
IllegalArgumentException
如果延迟小于或等于零
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.