The use of timer in Java--reprint of the network

Source: Internet
Author: User

The usage is simple, new a timer, and then write a timertask subclass.

ImportJava.util.Timer; ImportJava.util.TimerTask;  Public classM { Public Static voidMain (string[] args) {//TODO todo.generated by ZoerTimer timer =NewTimer (); Timer.schedule (NewMyTask (), 1000, 2000); }  }    classMyTaskextendsTimerTask {@Override Public voidrun () {System.out.println ("DDDD"); }    }  

In this way, you can start executing mytask after 1 seconds, once every two seconds.

Of course, the function of a timer can also be constructed by itself, and then by using sleep in the thread to simulate stopping for a period of time before performing an action.

In fact, look at the source of TimerTask can immediately know, TimerTask is to achieve the Runnable interface. In other words, a timer to perform an operation over a period of time is also done by a thread.

The use of timer in Java--reprint of the network

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.