定时任务调度工作(学习记录 三)timer其他重要函数
TimerTask的两个重要函数:
1.cancel()
作用:
取消当前TimerTask里的任务
演示:
先在继承了TimerTask的类中添加一个计时器,然后在run方法中合适的位置添加cancel()方法:
public class MyTimerTask extends TimerTask{ private String name; //计时器 private Integer count = 0; public MyTimerTask(String inputName) { name = inputName; } @Override public void run() { if(count < 3) { //以yyyy-MM-dd HH:mm:ss的格式打印当前执行时间 //如2019-4-22 00:00:00 Calendar calendar = Calendar.getInstance(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //打印当前name的内容 System.out.println("Current exec name is:" + name + "--------------" + sf.format(calendar.getTime())); count ++; }else { cancel(); System.out.println("Task cancel!"); } } public String getName() { return name; } public void setName(String name) { this.name = name; } }
接着在之前建的MyTimer类下面执行
/*等待delay毫秒后首次执行task 之后每隔period毫秒重复执行一次task 如现在是2019-4-22 00:00:00 则在2019-4-22 00:00:03第一次执行task:打印任务的名字 之后每隔两秒执行一次task*/ myTimerTask.setName("schedule"); timer.schedule(myTimerTask,3000,2000);
执行效果如下:
2.scheduledExecutionTime()
作用:
返回此任务最近实际执行的已安排执行的时间,(例如计划三秒钟之后执行某任务,那么返回的时间就是三秒钟之后的时间)
返回值:
最近发生此任务执行安排的时间,为long型
演示:
myTimerTask.setName("schedule"); timer.schedule(myTimerTask,3000); System.out.println("scheduled time is" + sf.format(myTimerTask.scheduledExecutionTime()));
执行结果如下:
Timer的其他函数
1.cancel()
作用:终止此计时器,丢弃所有当前已安排的任务
演示:
public class CancelTest { public static void main(String[] args) throws InterruptedException { //创建Timer实例 Timer timer = new Timer(); //创建TimerTask实例 MyTimerTask task1 = new MyTimerTask("task1"); MyTimerTask task2 = new MyTimerTask("task2"); //获取当前的执行时间并打印 Date starTime = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("start time is:" + sf.format(starTime)); //task1首次执行是距离现在时间3秒后执行,之后每隔2秒执行一次; //task2首次执行是距离现在时间1秒后执行,之后每隔2秒执行一次; timer.schedule(task1, 3000,2000); timer.schedule(task2, 1000,2000); //休眠5秒 Thread.sleep(5000); //获取当前的执行时间并打印 Date cancelTime = new Date(); System.out.println("cancel time is:" + sf.format(cancelTime)); //取消所有任务 timer.cancel(); System.out.println("Task all canceled!"); } }
执行结果:
purge():
作用:
从此计时器的任务队列中移除所有已取消的任务
返回值:
从队列中移除的任务数
演示:
public class CancelTest { public static void main(String[] args) throws InterruptedException { //创建Timer实例 Timer timer = new Timer(); //创建TimerTask实例 MyTimerTask task1 = new MyTimerTask("task1"); MyTimerTask task2 = new MyTimerTask("task2"); //获取当前的执行时间并打印 Date starTime = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("start time is:" + sf.format(starTime)); //task1首次执行是距离现在时间3秒后执行,之后每隔2秒执行一次; //task2首次执行是距离现在时间1秒后执行,之后每隔2秒执行一次; timer.schedule(task1, 3000,2000); timer.schedule(task2, 1000,2000); System.out.println("current canceled task number is:" + timer.purge()); //休眠2秒 Thread.sleep(2000); //获取当前的执行时间并打印 Date cancelTime = new Date(); System.out.println("cancel time is:" + sf.format(cancelTime)); //取消任务,这里用的是Task的cancel方法,而不是Timer的cancel方法 task2.cancel(); System.out.println("current canceled task number is:" + timer.purge()); } }
执行效果: