import java.util.Date;
import java.util.TimerTask;
public class CustomTimerTask extends TimerTask {
public Date setDate = null;
public CustomTimerTask() {
}
public CustomTimerTask(Date setDate) {
this.setDate = setDate;
}
@Override
public void run() {
if (setDate != null) {
Date date = new Date(System.currentTimeMillis());
if (date.toLocaleString().equals(setDate.toLocaleString())) {
System.out.println(date.toLocaleString());
}
}
}
}
public class StartTask {
public static void main(String[] args){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timer timer=new Timer();
//第一个任务的时间
Date setDate=format.parse("2012-7-10 15:51:05");
timer.schedule(new CustomTimerTask(setDate),setDate);
//第二个任务的时间
setDate=format.parse("2012-7-10 15:51:25");
timer.schedule(new CustomTimerTask(setDate),setDate);
}
}
java TimerTask一般说明
最新推荐文章于 2021-03-07 00:08:22 发布
本文介绍了一个使用Java实现的自定义定时任务示例。通过继承`TimerTask`类并覆盖`run`方法来创建定时任务,可以根据设定的时间点触发特定的操作。示例中展示了如何安排两个不同的任务,在指定的时间点执行。
955

被折叠的 条评论
为什么被折叠?



