Java中タイマー
6027 ワード
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimeTest {
public static void main(String[] args) {
timer1();
//timer2();
//timer3();
//timer4();
}
// : task time schedule(TimerTask task, Date time)
public static void timer1() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("------- --------");
}
}, 2000);// time, 2000
}
// : task delay peroid
// schedule(TimerTask task, long delay, long period)
public static void timer2() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("------- --------");
}
}, 1000, 5000);
}
// : task delay peroid 。
// scheduleAtFixedRate(TimerTask task, long delay, long period)
public static void timer3() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("------- --------");
}
}, 1000, 2000);
}
// : task firstTime period .
// Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
public static void timer4() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12); //
calendar.set(Calendar.MINUTE, 0); //
calendar.set(Calendar.SECOND, 0); //
Date time = calendar.getTime(); // , 12:00:00
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("------- --------");
}
}, time, 1000 * 60 * 60 * 24);//
}
}