Implementation of Timer __java in Java

Source: Internet
Author: User

Timer class timer in the Java.util package. When used, first instantiate, then use the instance's schedule (timertask task, long delay) method to set the specified task tasks to execute after the specified delay delay. The timer task Class TimerTask is an abstract class that inherits and overrides its run () method to achieve specific tasks.

Schedule (timertask task, Date time) specifies that task tasks are executed at a specified time.

The Cancel () method ends this timer.

Schedule (timertask task, long delay, long period) method setting specifies that task tasks perform a fixed delay delay after specifying a delay peroid.

Scheduleatfixedrate (timertask task, long delay, long period) method setting specifies that task tasks perform a fixed frequency delay after specifying a delay peroid.

To implement a timed task, the timer and TimerTask classes in Java can be very easy to implement real-time call processing functions. These two classes are easy to use and can accomplish most of our needs for timers.


Look at a simple example: according to the time set by the configuration file doxxx

The configuration file is as follows: Time.properties

#delay执行任务前的延迟时间
#internal执行各后续任务之间的时间间隔
Delay=1
Internal=3

The code is as follows:
/** * Timer * @author Zhoucan * * * * * * */public class Time {Private timer timer, public time () {timer = new timer ();} private T Imertask task = new TimerTask () {public void run () {//doxxx System.out.println (' Do Me ');}; /** * Start Timer */public void start () {InputStream is = This.getclass (). getResourceAsStream ("/time.properties"); Properties Pro = new properties (); try {pro.load (is);} catch (IOException e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Read configuration file failed!"); int delay = Integer.parseint (Pro.getproperty ("delay")); int internal = Integer.parseint (Pro.getproperty ("internal")); Timer.schedule (task, delay*1000,internal*1000); public static void Main (string[] args {Time time = new Time (); Time.start ();}}

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.