The first type:
ImportJava.util.Timer; ImportJava.util.TimerTask; /*** * In the first way compared to the advantage 1> when you start and go to cancel a task can control 2> the first time you perform a task can specify the delay time you want * * When implemented, the timer class can dispatch tasks, TimerTask is implemented in the run () method Specific tasks. A timer instance can dispatch multiple tasks, which are thread-safe. * When the timer's constructor is called, it creates a thread that can be used to dispatch the task. Here is the code: * *@authorEasonjim **/ Public classTask2 { Public Static voidMain (string[] args) {timertask task=NewTimerTask () {@Override Public voidrun () {//task to run goes hereSYSTEM.OUT.PRINTLN ("Hello!!!"); } }; Timer Timer=NewTimer (); LongDelay = 0; LongIntevalperiod = 1 * 1000; //Schedules the task to being run in an intervaltimer.scheduleatfixedrate (task, delay, intevalperiod); } //End of main}
The second type:
ImportJava.util.Timer; ImportJava.util.TimerTask; /*** * In the first way compared to the advantage 1> when you start and go to cancel a task can control 2> the first time you perform a task can specify the delay time you want * * When implemented, the timer class can dispatch tasks, TimerTask is implemented in the run () method Specific tasks. A timer instance can dispatch multiple tasks, which are thread-safe. * When the timer's constructor is called, it creates a thread that can be used to dispatch the task. Here is the code: * *@authorEasonjim **/ Public classTask2 { Public Static voidMain (string[] args) {timertask task=NewTimerTask () {@Override Public voidrun () {//task to run goes hereSYSTEM.OUT.PRINTLN ("Hello!!!"); } }; Timer Timer=NewTimer (); LongDelay = 0; LongIntevalperiod = 1 * 1000; //Schedules the task to being run in an intervaltimer.scheduleatfixedrate (task, delay, intevalperiod); } //End of main}
The third type:
Importjava.util.concurrent.Executors; ImportJava.util.concurrent.ScheduledExecutorService; ImportJava.util.concurrent.TimeUnit; /*** * * * scheduledexecutorservice is from Java SE5 java.util.concurrent, as a concurrent tool class was introduced, this is the most ideal timing task implementation method. * Compared to the previous two methods, it has the following advantages: * 1> is a thread pool way to perform tasks compared to the single thread of the timer * 2> can be very flexible to set the first time to perform a task delay * 3> provides a good convention to set the time interval for execution * * Below is the implementation code, we show this example through Scheduledexecutorservice#scheduleatfixedrate, through the control of the parameters in the code, the first execution added delay time. * * * @authorEasonjim **/ Public classTASK3 { Public Static voidMain (string[] args) {Runnable Runnable=NewRunnable () { Public voidrun () {//task to run goes hereSYSTEM.OUT.PRINTLN ("Hello!!"); } }; Scheduledexecutorservice Service=executors. Newsinglethreadscheduledexecutor (); //The second parameter is the delay time for the first execution, and the third parameter is the time interval for the scheduled executionService.scheduleatfixedrate (runnable, 10, 1, Timeunit.seconds); } }
Reference:
http://blog.csdn.net/haorengoodman/article/details/23281343/(the above content is transferred from this article)
Pure Java Implementation timed tasks (RPM)