Java comes with a timer that has two important classes: TimerTask and timer. As follows:
Simple to use:
Package Com;import Java.util.date;import Java.util.timer;public class Timertest extends timer{public static void main ( String[] args) throws interruptedexception {Task task = new Task (); Timer QUARTZ2 = new Timer ("task", true);//Daemon thread
Quartz2.schedule (Task, New Date ()); System.out.println ("***************************"); Thread.Sleep (10);//main thread hibernation 10ms, the task took the opportunity to execute. }}
Package Com;import Java.util.timertask;public Class Task extends TimerTask {int i = 0; @Overridepublic void Run () {SYSTEM.O UT.PRINTLN ("Thread " + thread.currentthread (). GetId () + ", \ T" + thread.currentthread (). GetName () + "\ t start execution ... ") while (true) {System.out.println (" executed: \ t "+ i +" \ T "), i++;if (i = =) {System.out.println (" ===================== "+ I ), Break;}}}
Execution Result:
Java Timer Timer Usage