three ways to implement timed tasks in Java2014-04-09 16:57 142287 People read Comments (4) Collection report /c3> This article has been included in:
Classification:Timerjava(+) Author of similar articlesX
/** * Normal Thread * This is the most common, create a thread, and then let it run in the while loop, * through the sleep method to achieve the effect of timed tasks. This can be implemented quickly and easily with the following code: * @author GT * */public class Task1 {public static void main (string[] args) {//run in a secondfinal lon G timeinterval = 1000; Runnable Runnable = new Runnable () {public void run () {when (true) {//-------code for Task to RunSystem.out.println ("He Llo! "); /-------ends Heretry {thread.sleep (timeinterval);} catch (Interruptedexception e) {e.printstacktrace ();}}}; Thread thread = new Thread (runnable); Thread.Start ();}}
Import Java.util.timer;import java.util.timertask;/** * * in the first way compared to the advantage 1> when starting and going to cancel the task can be controlled 2> You can specify the delay time you want when you perform a task for the first time * * When implemented, the timer class can dispatch tasks, timertask by implementing specific tasks in the run () method. 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: * * @author GT * */public class Task2 {public static void main (string[] args) {timertask task = new Timerta SK () {@Overridepublic void Run () {//task to run goes hereSystem.out.println ("Hello!!!");}}; Timer timer = new timer (); Long delay = 0;long Intevalperiod = 1 * 1000;//schedules the task to is run in an Intervaltimer . scheduleatfixedrate (Task, delay, intevalperiod);} End of Main}
Import Java.util.concurrent.executors;import Java.util.concurrent.scheduledexecutorservice;import java.util.concurrent.timeunit;/** * * * scheduledexecutorservice is from Java SE5 in the 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 benefits: * 1> compared to the single thread of the timer, it is through the thread pool way to perform the task of * 2> can be very flexible to set the first time to perform the task delay time * 3> provides a good contract, In order 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 adds delay time. * * * @author GT * */public class Task3 {public static void main (string[] args) {Runnable Runnable = new Runna BLE () {public void run () {//task to run goes hereSystem.out.println ("Hello!!");}}; Scheduledexecutorservice service = Executors.newsinglethreadscheduledexecutor ();//The second parameter is the delay time of the first execution, The third parameter is the time interval for timed execution service.scheduleatfixedrate (runnable, 1, timeunit.seconds);}}
Java Timer comparison