Custom timer (timer)

Source: Internet
Author: User
Tags time in milliseconds

Recently, I used the java. util. Timer class in my project. I personally feel good. However, Timer classes cannot meet project requirements in some aspects. For example, when timer is used, its cycle cannot be changed after schedule () method is called (initialized, the cycle can be changed only when the cancel () method is called and then restarted. You have customized a timer thread to enable, disable, and dynamically change the cycle. The specific code is as follows:

/*** @ Classname: mytimer * @ Description: Todo custom Timer class * @ author YC * @ date 10:42:04 on January 1, October 16, 2014 */package COM. keymantek. demo; public class mytimer extends thread {// toggle control flag public static Boolean flag = false; // start time // delay in milliseconds before task is to be executed. public static long starttime = 1000*3 * 60l; // cycle time // time in milliseconds between successive task executions. public static long period = 1000*5 * 60l; @ overridepublic void run () {While (true) {try {// start time thread. sleep (starttime); // collect relevant information only when the flag is true. While (FLAG) {// business logic processing block // Loop Time Thread. sleep (period);} // when the flag is false, the thread is resting. sleep (1000l);} catch (interruptedexception e) {e. printstacktrace ();}}}}
The following is the control class provided for this thread:

/*** @ Classname: timerreadelectricitymetertable * @ Description: Todo custom timer thread control class * @ author YC * @ date 10:41:29 on January 1, October 16, 2014 */package COM. keymantek. demo; public class mytimercontroller {/*** @ category initialize custom scheduled thread */public static void initmytimer () {mytimer timer = new mytimer (); timer. start (); system. out. println ("initialize custom scheduled thread");}/*** @ category enable custom scheduled thread * @ Param period cycle time */public static void startmytimer (long period) {mytimer. flag = true; mytimer. period = period; system. out. println ("enable custom scheduled thread");}/*** @ category stop custom scheduled thread */public static void stopmytimer () {mytimer. flag = false; system. out. println ("Stop custom scheduled thread ");}}

It is constantly improving. If any bug is found, please make comments!


Custom timer (timer)

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.