Start, stop, restart of tasks in Java timer

Source: Internet
Author: User

 PackageCom.cvicse.ump.timer.service;Importjava.util.Date;ImportJava.util.Timer;Importcom.cvicse.ump.timer.task.TimTaskTest;/*** Timed Task Manager *@authorDyh **/ Public classTimermanager {/*** Single case mode*/    Private StaticTimermanager Timermanager =NULL; PrivateTimermanager () {} Public StaticTimermanager getinstance () {if(Timermanager = =NULL) {Timermanager=NewTimermanager (); }        returnTimermanager; }        /*** Timer*/    PrivateTimer timer =NewTimer ("Homepagetimer"); /*** Scheduled Tasks*/    PrivateTimtasktest TimerTask =NULL; /*** Start a scheduled task*/     Public voidStarttimertask () {timer.purge (); if(timertask==NULL) {TimerTask=Newtimtasktest (); } timer.schedule (TimerTask,NewDate (), 5000); }        /*** Scheduled Task cancellation*/     Public voidStoptimertask () {timertask.cancel (); TimerTask=NULL;//exception is reported if new is not re-    }}

The Cancel () method of the TimerTask class and the Timer class

Either the TimerTask class or the Timer class, the Cancel () method is used to clear tasks in the task queue.

Although all tasks in the task queue are purged, there are some differences: the Cancel () method in the TimerTask class focuses on purging itself from the task queue, while other tasks are not affected, while the Cancel () method in the Timer class empties all tasks in the task queue.

The following is a demonstration of a specific example.
The Cancel () method test in the TimerTask class:

ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.Timer;ImportJava.util.TimerTask; Public classTest { Public Static voidMain (string[] args)throwsinterruptedexception {System.out.println ("Current time is:" +NewDate ()); Calendar C=calendar.getinstance (); Date runDate1=C.gettime (); System.out.println ("Plan Time:" +runDate1); Mytaska Task1=NewMytaska (); MYTASKB Task2=NewMYTASKB (); Timer Timer=NewTimer (); Timer.schedule (Task1, runDate1,4000); Timer.schedule (Task2, runDate1,4000); }}classMytaskaextendsTimerTask {@Override Public voidrun () {System.out.println ("A Run timer=" +NewDate ());  This. Cancel ();//The Cancel () method in the TimerTask class is calledSYSTEM.OUT.PRINTLN ("a task itself removes itself, b tasks are unaffected, continue to run"); }}classMytaskbextendsTimerTask {@Override Public voidrun () {System.out.println ("B Run timer=" +NewDate ()); }}

The Cancel () method test in the Timer class:

ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.Timer;ImportJava.util.TimerTask; Public classTest { Public Static voidMain (string[] args)throwsinterruptedexception {System.out.println ("Current time is:" +NewDate ()); Calendar C=calendar.getinstance (); Date runDate1=C.gettime (); System.out.println ("Plan Time:" +runDate1); Mytaska Task1=NewMytaska (); MYTASKB Task2=NewMYTASKB (); Timer Timer=NewTimer (); Timer.schedule (Task1, runDate1,4000); Timer.schedule (Task2, runDate1,4000); Thread.Sleep (12000);         Timer.cancel (); System.out.println ("A, b tasks are removed"); }}classMytaskaextendsTimerTask {@Override Public voidrun () {System.out.println ("A Run timer=" +NewDate ()); }}classMytaskbextendsTimerTask {@Override Public voidrun () {System.out.println ("B Run timer=" +NewDate ()); }}

* * Note: the Cancel () method in the **timer class sometimes does not necessarily stop the scheduled task, but executes normally.

Start, stop, restart of tasks in Java timer

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.