Four timer methods in java, four timer methods in java

Source: Internet
Author: User

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.
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.