java实现定时器的四种方式:
一、
class="java">/**
* 延迟20000毫秒执行 java.util.Timer.schedule(TimerTask task, long delay)
*/
public static void timer1() {
Timer nTimer = new Timer();
nTimer.schedule(new TimerTask() {
public void run() {
System.err.println("-------设定要指定任务--------");
}
}, 2000);
}
二、
/**
* java.util.Timer.schedule(TimerTask task, long delay)
*/
public static void timer2() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("-------延迟5000毫秒,每1000毫秒执行一次--------");
}
}, 5000, 1000);
}
三、
/**
* java.util.Timer.schedule(TimerTask task, long delay)
*/
public static void timer3() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.err.println("-------延迟5000毫秒,每1000毫秒执行一次--------");
}
}, 5000, 1000);
}
四、
/**
* 设置17:56执行任务
* java.util.Timer.scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
*/
public static void timer4() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 56);
calendar.set(Calendar.SECOND, 0);
Date time = calendar.getTime();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
}
}, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
}