Java timer usage (timer)

Source: Internet
Author: User

Source: http://www.cnblogs.com/wyqtomorrow/archive/2007/04/28/730978.aspx

1. In application development, periodic operations are often required, such as performing an operation every five minutes. The most convenient and efficient way to implement such operations is to use the java. util. Timer tool class.

Private java. util. Timer timer;
Timer = new timer (true );
Timer. Schedule (
New java. util. timertask () {public void run () {// server. checknewmail (); Method to operate}, 0, 5*60*1000 );
The first parameter is the method to be operated, the second parameter is to set the delay time, and the third parameter is the period setting. The operation is executed at intervals.

After using these lines of code, Timer itself will call the server. checknewmail () method every five minutes without starting the thread. Timer itself is also multi-threaded synchronization. Multiple Threads can share one timer without external synchronization code.

2,
(1) timer. Schedule (timertask task, date time) is scheduled to execute the specified task at the specified time.
(2) timer. Schedule (timertask task, date firsttime, long period) arranges the specified task to start repeated fixed delay execution at the specified time.
(3) timer. Schedule (timertask task, long delay) arranges to execute the specified task after the specified delay.
(4) timer. Schedule (timertask task, long delay, long period) schedules the specified task to start repeated fixed delay execution after the specified delay.
(5) timer. scheduleatfixedrate (timertask task, date firsttime, long period) Schedule the specified task to start repeating at a fixed rate.
(6) timer. scheduleatfixedrate (timertask task, long delay, long period) arranges the specified task to start repeated execution at a fixed rate after the specified delay.

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.