Four timer methods in java, four timer methods in java
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 ();
}
// Method 1: Set the specified task to execute schedule (TimerTask task, Date time) at the specified time)
Public static void timer1 (){
Timer timer = new Timer ();
Timer. schedule (new TimerTask (){
Public void run (){
System. out. println ("------- set the task to be specified --------");
}
}, 2000); // set the specified time, which is 2000 milliseconds.
}
// Method 2: Set the scheduled task to execute fixed-latency peroid after the specified delay.
// 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 ("------- set the task to be specified --------");
}
}, 1000,500 0 );
}
// Method 3: Set the specified task to execute a fixed frequency peroid after the specified delay.
// 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 ("------- set the task to be specified --------");
}
}, 1000,200 0 );
}
// Method 4: Schedule the specified task to start repeating at a fixed rate of period at the specified time firstTime.
// Timer. scheduleAtFixedRate (TimerTask task, Date firstTime, long period)
Public static void timer4 (){
Calendar calendar = Calendar. getInstance ();
Calendar. set (Calendar. HOUR_OF_DAY, 12); // when controlling
Calendar. set (Calendar. MINUTE, 0); // Control score
Calendar. set (Calendar. SECOND, 0); // seconds
Date time = calendar. getTime (); // obtain the task execution time, which is 12:00:00 today.
Timer timer = new Timer ();
Timer. scheduleAtFixedRate (new TimerTask (){
Public void run (){
System. out. println ("------- set the task to be specified --------");
}
}, Time, 1000*60*60*24); // The interval is fixed every day.
}
}