Pure Java implementation of timed tasks

Source: Internet
Author: User

The first type:

ImportJava.util.Timer; ImportJava.util.TimerTask; /*** * In the first way compared to the advantage 1> when you start and go to cancel a task can control 2> the first time you perform a task can specify the delay time you want * * When implemented, the timer class can dispatch tasks, TimerTask is implemented in the run () method Specific tasks.  A timer instance can dispatch multiple tasks, which are thread-safe. * When the timer's constructor is called, it creates a thread that can be used to dispatch the task. Here is the code: * *@authorEasonjim **/   Public classTask2 { Public Static voidMain (string[] args) {timertask task=NewTimerTask () {@Override Public voidrun () {//task to run goes hereSYSTEM.OUT.PRINTLN ("Hello!!!");          }          }; Timer Timer=NewTimer (); LongDelay = 0; LongIntevalperiod = 1 * 1000; //Schedules the task to being run in an intervaltimer.scheduleatfixedrate (task, delay, intevalperiod); } //End of main}

The second type:

ImportJava.util.Timer; ImportJava.util.TimerTask; /*** * In the first way compared to the advantage 1> when you start and go to cancel a task can control 2> the first time you perform a task can specify the delay time you want * * When implemented, the timer class can dispatch tasks, TimerTask is implemented in the run () method Specific tasks.  A timer instance can dispatch multiple tasks, which are thread-safe. * When the timer's constructor is called, it creates a thread that can be used to dispatch the task. Here is the code: * *@authorEasonjim **/   Public classTask2 { Public Static voidMain (string[] args) {timertask task=NewTimerTask () {@Override Public voidrun () {//task to run goes hereSYSTEM.OUT.PRINTLN ("Hello!!!");          }          }; Timer Timer=NewTimer (); LongDelay = 0; LongIntevalperiod = 1 * 1000; //Schedules the task to being run in an intervaltimer.scheduleatfixedrate (task, delay, intevalperiod); } //End of main}

The third type:

Importjava.util.concurrent.Executors; ImportJava.util.concurrent.ScheduledExecutorService; ImportJava.util.concurrent.TimeUnit; /*** * * * scheduledexecutorservice is from Java SE5 java.util.concurrent, as a concurrent tool class was introduced, this is the most ideal timing task implementation method.   * Compared to the previous two methods, it has the following advantages: * 1> is a thread pool way to perform tasks compared to the single thread of the timer * 2> can be very flexible to set the first time to perform a task delay * 3> provides a good convention to set the time interval for execution *  * Below is the implementation code, we show this example through Scheduledexecutorservice#scheduleatfixedrate, through the control of the parameters in the code, the first execution added delay time. *   *   * @authorEasonjim **/   Public classTASK3 { Public Static voidMain (string[] args) {Runnable Runnable=NewRunnable () { Public voidrun () {//task to run goes hereSYSTEM.OUT.PRINTLN ("Hello!!");          }          }; Scheduledexecutorservice Service=executors. Newsinglethreadscheduledexecutor (); //The second parameter is the delay time for the first execution, and the third parameter is the time interval for the scheduled executionService.scheduleatfixedrate (runnable, 10, 1, Timeunit.seconds); }  }  

Reference:

http://blog.csdn.net/haorengoodman/article/details/23281343/(the above content is transferred from this article)

Pure Java Implementation timed tasks (RPM)

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.