ScheduledExecutorService 将定时任务与线程池功能结合使用:当任务到达执行时间,将任务交于线程池,由线程池分配线程去执行任务。其中有两个方法容易混淆:scheduleAtFixedRate scheduleWithFixedDelay
区别:
ScheduleAtFixedRate 两次任务之间的间隔时间,取决于每次任务执行的时间长短;
ScheduleWithFixedDelay 不受任务执行时间长短影响,固定这次任务执行结束后隔x秒执行下一次。
首先看看这两个方法:
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);假如每个任务执行花费time时间,如果time>= period,则这次任务执行结束后 立刻执行下一次任务。
如果time<period , 则这次任务执行结束后 ,隔 period-time后执行下一次任务。
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);不管任务花费多少时间,当这次任务执行结束,一定要等delay之后,再执行下一次任务。
示例:
scheduleAtFixedRate
public class TestScheduledExecutorService1 {
public static void main(String[] args) {
ScheduledExecutorService
executorService = Executors.newSingleThreadScheduledExecutor();
Runnable run = new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + "begin == " + System.currentTimeMillis());
Thread.currentThread().sleep(3000); //任务花费大约3秒
System.out.println(Thread.currentThread().getName() + "end == " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
int delay = 1;
int timespan = 4;
executorService.scheduleAtFixedRate(run, delay, timespan, TimeUnit.SECONDS);
}}
执行结果:
pool-1-thread-1begin == 1559986475105
pool-1-thread-1end == 1559986478106
pool-1-thread-1begin == 1559986479105 //上一次执行结束后,隔1秒(4-3)执行下一次
pool-1-thread-1end == 1559986482105
pool-1-thread-1begin == 1559986483105
pool-1-thread-1end == 1559986486106.......
如果上面的Thread.currentThread().sleep(3000);改为 5000:
System.out.println(Thread.currentThread().getName() + "begin == " + System.currentTimeMillis());
Thread.currentThread().sleep(5000); //任务花费大约5秒
System.out.println(Thread.currentThread().getName() + "end == " + System.currentTimeMillis());
执行结果如下:
pool-1-thread-1begin == 1559986900174
pool-1-thread-1end == 1559986903175
pool-1-thread-1begin == 1559986903175 //上一次执行结束,立刻执行下一次,因为2-3<=0
pool-1-thread-1end == 1559986906176
pool-1-thread-1begin == 1559986906176
pool-1-thread-1end == 1559986909176
pool-1-thread-1begin == 1559986909176
......
scheduleWithFixedDelay
public class TestScheduledExecutorService1 {
public static void main(String[] args) {
ScheduledExecutorService
executorService = Executors.newSingleThreadScheduledExecutor();
Runnable run = new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + "begin == " + System.currentTimeMillis());
Thread.currentThread().sleep(3000); //每个任务花费约3秒
System.out.println(Thread.currentThread().getName() + "end == " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
int delay = 1;
int timespan = 4;
executorService.scheduleWithFixedDelay(run, delay, timespan, TimeUnit.SECONDS);
}
}
执行结果:
pool-1-thread-1begin == 1559986731262
pool-1-thread-1end == 1559986734262
pool-1-thread-1begin == 1559986738263 //上一次执行结束后,隔4秒执行下一次任务。
pool-1-thread-1end == 1559986741263
pool-1-thread-1begin == 1559986745264
pool-1-thread-1end == 1559986748265
.......
如果上面的Thread.currentThread().sleep(3000);改为 5000,执行结果:
pool-1-thread-1begin == 1560072366850
pool-1-thread-1end == 1560072371852
pool-1-thread-1begin == 1560072375852 //依旧是隔4秒执行下一次任务
pool-1-thread-1end == 1560072380852
pool-1-thread-1begin == 1560072384853
pool-1-thread-1end == 1560072389854
......
另外,ScheduledExecutorService 还有其他的方法,比如:
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
long delay, TimeUnit unit);
等待delay时间后执行一个实现了Callable的任务,返回执行结果。