Detailed description of Java scheduled task Timer schedule

Source: Internet
Author: User

Detailed description of Java scheduled task Timer schedule

Do not speak gossip. Go to the demo directly.

[Java]View plaincopy
  1. Public class TimerTask {
  2. Public static int I = 1;
  3. Public static class TestTask extends TimerTask {
  4. Public void run (){
  5. System. out. println ("the" + I ++ "times scheduled task ");
  6. }
  7. }
  8. Public static void main (String [] args) throws ParseException {
  9. Date date = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"). parse ("10:27:00 ");
  10. Date firstTime = date;
  11. Long delay = 6000;
  12. Long period = 3000;
  13. Timer timer1 = new Timer ();
  14. Timer timer2 = new Timer ();
  15. Timer timer3 = new Timer ();
  16. Timer timer4 = new Timer ();
  17. TestTask task = new TestTask ();
  18. // Timer1.schedule (task, date );
  19. // Timer2.schedule (task, delay );
  20. // Timer3.schedule (task, firstTime, period );
  21. Timer4.schedule (task, delay, period );
  22. }
  23. }

    The Code defines a scheduled task TestTask and creates four timers. Each timer uses the schedule overload method.

    Timer1 is used to execute the scheduled task at the specified time date.

    Timer2 executes the scheduled task after the delay of delay in milliseconds.

    Timer3 starts at firstTime and runs the scheduled task once every interval of period milliseconds.

    Timer4 is a scheduled task executed after the delay of delay in milliseconds, and is executed once every interval of period milliseconds.

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.