其他重要函数
- TimeTask的两个重要函数
- cancel()
作用:取消当前TimeTask里的任务
如下代码定义TimerTask
package lxd.timer.demo;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;
/**
*
* 任务调度timer demo.
*
* <pre>
* 任务调度timer demo.
* </pre>
*
* @author 李晓东
*
* 2017.05.21
*
* @since 1.0
*
*/
public class MyTimerTask extends TimerTask {
private String name;
private Integer count = 0;
public MyTimerTask(String name) {
this.name = name;
}
@Override
public void run() {
if (count < 3) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("current exec time is: " + sf.format(calendar.getTime()));
System.out.println("current timeTask name: " + name);
count++;
} else {
cancel();
System.out.println("Task cancel!");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试demo,main函数中代码:
//1.创建Timer实例
Timer timer = new Timer();
//2.创建TimerTask
MyTimerTask timerTask = new MyTimerTask("No.1");
/*
* 获取当前时间,并设置成距离当前时间三秒之后的时间
*
*/
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("timer 类中 当前时间:" + sf.format(calendar.getTime()));
calendar.add(Calendar.SECOND, 3);
timerTask.setName("schedule");
timer.schedule(timerTask, 3000, 2000);
测试结果:
timer 类中 当前时间:2017-05-28 12:03:41
current exec time is: 2017-05-28 12:03:44
current timeTask name: schedule
current exec time is: 2017-05-28 12:03:46
current timeTask name: schedule
current exec time is: 2017-05-28 12:03:48
current timeTask name: schedule
Task cancel!
- scheduledExecutionTime()
作用:返回此任务最近实际执行的已安排执行的时间,返回值为long型
测试demo
//1.创建Timer实例
Timer timer = new Timer();
//2.创建TimerTask
MyTimerTask timerTask = new MyTimerTask("No.1");
/*
* 获取当前时间,并设置成距离当前时间三秒之后的时间
*
*/
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("timer 类中 当前时间:" + sf.format(calendar.getTime()));
calendar.add(Calendar.SECOND, 3);
timerTask.setName("schedule");
timer.schedule(timerTask, 3000);
System.out.println("scheduled time is: " + sf.format(timerTask.scheduledExecutionTime()));
测试结果:
timer 类中 当前时间:2017-05-28 12:09:09
scheduled time is: 2017-05-28 12:09:12
current exec time is: 2017-05-28 12:09:12
current timeTask name: schedule
2.Timer下的其他函数
- cancel()
作用:终止此计时器,丢弃所有当前已安排的任务
- purge()
作用:从此计时器的任务队列中移除所有已取消的任务。返回值:从队列中移除的任务数。
demo:
package lxd.timer.demo;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
/**
*
* 取消定时任务实体类
*
* <pre>
* 取消定时任务实体类
* </pre>
*
* @author 李晓东
*
* 2017.05.28
*
* @since 1.0
*
*/
public class CancelTest {
public static void main(String[] args) throws InterruptedException {
Timer timer = new Timer();
//创建TimeTask实例
MyTimerTask task1 = new MyTimerTask("task1");
MyTimerTask task2 = new MyTimerTask("task2");
//获取当前的执行时间并打印
Date startTime = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("start time is: " + sf.format(startTime));
timer.schedule(task1, 3000, 2000);
timer.schedule(task2, 1000, 2000);
System.out.println("current canceled task number is: " + timer.purge());
//休眠5秒
Thread.sleep(2000);
//获取当前的执行时间并打印
Date cancelTime = new Date();
System.out.println("cancel time is: " + sf.format(cancelTime));
//取消所有任务
// timer.cancel();
// System.out.println("Task all canceled!");
//取消其中一个任务
task2.cancel();
System.out.println("current canceled task number is: " + timer.purge());
}
}