Java Timer comparison

Source: Internet
Author: User

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

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.