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