慕的地6384686
2017-05-17 22:41
有源码吗?
package com.may.seventy;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimerTask;
/**
* Created by junfeng on 17/5/17.
*/
public class MyTimerTask extends TimerTask {
private String name;
public MyTimerTask(String inputName){
name = inputName;
}
@Override
public void run() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current exec name is:"+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}package com.may.seventy;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
/**
* Created by junfeng on 17/5/17.
*/
public class MyTimer {
public static void main(String[] args) {
Timer timer = new Timer();
MyTimerTask myTimerTask = new MyTimerTask("NO.1");
//timer.schedule(myTimerTask,1000L,5000l);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sf.format(calendar.getTime()));
calendar.add(Calendar.SECOND,3);
// myTimerTask.setName("test1");
// timer.schedule(myTimerTask,calendar.getTime());
// myTimerTask.setName("test2");
// timer.schedule(myTimerTask,calendar.getTime(),3000);
myTimerTask.setName("test3");
timer.schedule(myTimerTask,2000);
}
}Java定时任务调度工具详解之Timer篇
23909 学习 · 33 问题
相似问题