import Java.util.Calendar; Import Java.util.Date; Import Java.util.Timer; Import Java.util.TimerTask; Public classTest { Public Static voidMain (string[] args) {//timer1 (); Timer2 (); //Timer3 (); //timer4 (); } //The first method: set the specified task tasks to execute schedule (timertask task, Date time) at specified times Public Static voidtimer1 () {Timer timer=NewTimer (); Timer.schedule (NewTimerTask () { Public voidrun () {System. out. println ("-------Set the task to be specified--------"); } }, -);//set the time specified, here ismilliseconds </a> } //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 (); Timer.schedule (NewTimerTask () { Public voidrun () {System. out. println ("-------Set the task to be specified--------"); } }, +, +); } //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) Public Static voidTimer3 () {Timer timer=NewTimer (); Timer.scheduleatfixedrate (NewTimerTask () { Public voidrun () {System. out. println ("-------Set the task to be specified--------"); } }, +, -); } //Fourth method: Schedule the specified task to execute at a fixed rate period the specified time Firsttime begins to repeat. //timer.scheduleatfixedrate (timertask task,date firsttime,long period) Public Static voidtimer4 () {Calendar Calendar=calendar.getinstance (); Calendar.Set(Calendar.hour_of_day, A);//Control TimeCalendar.Set(Calendar.minute,0);//Control PointsCalendar.Set(Calendar.second,0);//Control secondsDate Time= Calendar.gettime ();//The time to execute the task, here is today's 12:00:00Timer Timer=NewTimer (); Timer.scheduleatfixedrate (NewTimerTask () { Public voidrun () {System. out. println ("-------Set the task to be specified--------"); }}, Time, +* -* -* -);//This setting will delay the execution of the daily fixed } }
Java implementation timed tasks