1 PackageCom.lid;2 3 ImportJava.util.Calendar;4 Importjava.util.Date;5 ImportJava.util.Timer;6 ImportJava.util.TimerTask;7 8 //The first method: set the specified task tasks to execute schedule (timertask task, Date time) at specified times9 Public Static voidtimer1 () {TenTimer timer =NewTimer (); OneTimer.schedule (NewTimerTask () { A Public voidrun () { -System.out.println ("-------set to specify task--------"); - } the}, 2000);//sets the time specified, here is 2000 milliseconds - } - - //The second method: Set the execution of a fixed delay peroid for the specified task tasks after specifying delay delays + //Schedule (timertask task, long delay, long period) - Public Static voidTimer2 () { +Timer timer =NewTimer (); ATimer.schedule (NewTimerTask () { at Public voidrun () { -System.out.println ("-------set to specify task--------"); - } -}, 1000, 1000); - } - in //The third method: Set the execution of the fixed frequency peroid of the specified task tasks after specifying the delayed delay. - //scheduleatfixedrate (timertask task, long delay, long period) to Public Static voidTimer3 () { +Timer timer =NewTimer (); -Timer.scheduleatfixedrate (NewTimerTask () { the Public voidrun () { *System.out.println ("-------set to specify task--------"); $ }Panax Notoginseng}, 1000, 2000); - } the + //Fourth method: Schedule the specified task to execute at a fixed rate period the specified time Firsttime begins to repeat. A //timer.scheduleatfixedrate (timertask task,date firsttime,long period) the Public Static voidtimer4 () { +Calendar Calendar =calendar.getinstance (); -Calendar.set (Calendar.hour_of_day, 12);//Control Time $Calendar.set (calendar.minute, 0);//Control Points $Calendar.set (Calendar.second, 0);//Control seconds - -Date time = Calendar.gettime ();//The time to execute the task, here is today's 12:00:00 the -Timer timer =NewTimer ();WuyiTimer.scheduleatfixedrate (NewTimerTask () { the Public voidrun () { -System.out.println ("-------set to specify task--------"); Wu } -}, time, 1000 * 60 * 60 * 24);//This setting will delay the execution of the daily fixed About } $}
(RPM) Several uses of Java timers