Timers in Java

Source: Internet
Author: User

Note: The content is from someone else's blog, hereby declares

I. Timers in Java

The 2 important classes to implement the timer function in Java are

    • Timer class: Timer class, need an instance of TimerTask class as parameter;
    • TimerTask: Timer task class, the task to be performed by the timer is defined in the Run method of the class.

Two. Two ways to implement a timer

1. Common implementations (as opposed to being implemented by internal classes)

1 //write a class first2  Public classTimetest {3  Public Static voidMain (string[] args) {4     5Timer timer =NewTimer ();6Timer.schedule (NewMyTask (), 1000,2000);7 }8 9 //and then write a class .Ten  Public classMyTaskextendstimertask{ One  A @Override -      Public voidrun () { -System.out.println ("Start Running");  the     } -}

2. Through the Inner class implementation

  

1  PackageCom.lid;2 3 ImportJava.util.Calendar;4 Importjava.util.Date;5 ImportJava.util.Timer;6 ImportJava.util.TimerTask;7 8  Public classTest {9      Public Static voidMain (string[] args) {Ten         //timer1 (); One Timer2 (); A         //Timer3 (); -         //timer4 (); -     } the  -     //The first method: set the specified task tasks to execute schedule (timertask task, Date time) at specified times -      Public Static voidtimer1 () { -Timer timer =NewTimer (); +Timer.schedule (NewTimerTask () { -              Public voidrun () { +System.out.println ("-------set to specify task--------"); A             } at}, 2000);//sets the time specified, here is 2000 milliseconds -     } -  -     //The second method: Set the execution of a fixed delay peroid for the specified task tasks after specifying delay delays -     //Schedule (timertask task, long delay, long period) -      Public Static voidTimer2 () { inTimer timer =NewTimer (); -Timer.schedule (NewTimerTask () { to              Public voidrun () { +System.out.println ("-------set to specify task--------"); -             } the}, 1000, 1000); *     } $ Panax Notoginseng     //The third method: Set the execution of the fixed frequency peroid of the specified task tasks after specifying the delayed delay.  -     //scheduleatfixedrate (timertask task, long delay, long period) the      Public Static voidTimer3 () { +Timer timer =NewTimer (); ATimer.scheduleatfixedrate (NewTimerTask () { the              Public voidrun () { +System.out.println ("-------set to specify task--------"); -             } $}, 1000, 2000); $     } -     -     //Fourth method: Schedule the specified task to execute at a fixed rate period the specified time Firsttime begins to repeat.  the     //timer.scheduleatfixedrate (timertask task,date firsttime,long period) -      Public Static voidtimer4 () {WuyiCalendar Calendar =calendar.getinstance (); theCalendar.set (Calendar.hour_of_day, 12);//Control Time -Calendar.set (calendar.minute, 0);//Control Points WuCalendar.set (Calendar.second, 0);//Control seconds -  AboutDate time = Calendar.gettime ();//The time to execute the task, here is today's 12:00:00 $  -Timer timer =NewTimer (); -Timer.scheduleatfixedrate (NewTimerTask () { -              Public voidrun () { ASystem.out.println ("-------set to specify task--------"); +             } the}, time, 1000 * 60 * 60 * 24);//This setting will delay the execution of the daily fixed -     } $}

Timers in Java

Related Article

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.