上一篇文章说过Timer的缺陷,并且要用ScheduledExecutorService代替Timer执行任务,这次就说一下使用
一:简介
ScheduledExecutorService是ExecutorService接口的扩展
schedule(Callable<V> callable, long delay, TimeUnit unit) | 创建并执行在给定延迟后启用的 ScheduledFuture |
schedule(Runnable command, long delay, TimeUnit unit) | 创建并执行在给定延迟后启用的一次性操作 |
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnitunit) | 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。 |
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit) | 创建并执行一个在给定初始延迟后首次启用的定期操作,随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟 |
schedule方法被用来延迟指定时间来执行某个指定任务。如果你需要周期性重复执行定时任务可以使用scheduleAtFixedRate或者scheduleWithFixedDelay方法,它们不同的是前者以固定频率执行,后者以相对固定频率执行。
不管任务执行耗时是否大于间隔时间,scheduleAtFixedRate和scheduleWithFixedDelay都不会导致同一个任务并发地被执行。唯一不同的是scheduleWithFixedDelay是当前一个任务结束的时刻,开始结算间隔时间,如0秒开始执行第一次任务,任务耗时5秒,任务间隔时间3秒,那么第二次任务执行的时间是在第8秒开始。
ScheduledExecutorService的实现类,是ScheduledThreadPoolExecutor。ScheduledThreadPoolExecutor对象包含的线程数量是没有可伸缩性的,只会有固定数量的线程。不过你可以通过其构造函数来设定线程的优先级,来降低定时任务线程的系统占用。
特别提示:通过ScheduledExecutorService执行的周期任务,如果任务执行过程中抛出了异常,那么过ScheduledExecutorService就会停止执行任务,且也不会再周期地执行该任务了。所以你如果想保住任务都一直被周期执行,那么catch一切可能的异常。
二:直接上代码
package com.fuxl.main;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledThreadPoolExecutorTest {
private static long start;
public static void main(String[] args) {
ScheduledExecutorService timer = Executors.newScheduledThreadPool(2);
//创建任务
TimerTask task1 = new TimerTask() {
public void run() {
System.out.println("task1 invoked ! "+ (System.currentTimeMillis() - start));
try
{
Thread.sleep(5000); //休息5秒
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
//创建任务
TimerTask task2 = new TimerTask() {
public void run() {
System.out.println("task2 invoked ! "+ (System.currentTimeMillis() - start));
}
};
//执行任务
start = System.currentTimeMillis();
//task1:任务 1000:延迟时间 1000:循环时间 TimeUnit.MILLISECONDS:执行时间单位
timer.scheduleAtFixedRate(task1, 1000,1000, TimeUnit.MILLISECONDS);
timer.scheduleAtFixedRate(task2, 3000,1000, TimeUnit.MILLISECONDS);
}
}
参考:https://blog.csdn.net/u013851082/article/details/70207640