Java Timers Schedule and scheduleatfixedrate differences

Source: Internet
Author: User

Package Cn.lonecloud.test;import Java.util.date;import Java.util.timer;import java.util.timertask;/** * * @Title: Test02.java * @Package cn.lonecloud.test * @Description: * @author lonecloud * @date August 5, 2016 PM 4:43:29 */public class T est02 {public static void main (string[] args) throws Exception{timer3 ();  /** * Scheduleatfixedrate * For this, the time here is based on your incoming time. * No matter if you have a thread to hibernate or not, start executing * @Description: * @throws Exception */Public        static void Timer3 () throws exception{Date date = new Date ();        Timer timer = new timer ();        Thread.Sleep (9000); Timer.scheduleatfixedrate (New TimerTask () {public void run () {SYSTEM.OUT.PRINTLN (n            EW Date ());    }}, date, 1000); /** * Run results after 9 seconds * Fri 16:46:41 CST 2016Fri 16:46:41 CST 2016Fri-16:46:41 CST 2016Fri Au G-16:46:41 CST 2016Fri 16:46:41 CST 2016Fri 16:46:41 CST 2016Fri 16:46:41 CST 2016Fri 05 16:46 : 2016Fri CST16:46:41 CST 2016Fri 16:46:41 CST 2016Fri 16:46:42 CST 2016Fri 16:46:43 CST 2016Fri 05 16: 46:44 CST 2016Fri 16:46:45 CST 2016 */}

Package Cn.lonecloud.test;import Java.util.date;import Java.util.timer;import Java.util.timertask;public class Test01 {public static void main (string[] args) throws Exception {Timer2 ();} /** * Schedule * thread hibernation 9s after execution * @Description:  * @throws Exception *    /public static void Timer2 () throws Exception {
   date date = new Date ();    SYSTEM.OUT.PRINTLN (date);        Timer timer = new timer ();        Thread.Sleep (9000);        Timer.schedule (New TimerTask () {public            void run () {                System.out.println (new Date ());            }        }, Date, 1000) ;    }    /**     * Results:     *  Fri 16:48:56 CST 2016Fri 16:49:05 CST 2016Fri-16:49:06 CST 2016Fri 05 16:49:07 CST 2016Fri 16:49:08 CST 2016Fri 16:49:09 CST 2016Fri 16:49:10 CST     */}

Java Timers Schedule and scheduleatfixedrate differences

Related Article

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.