Set up timed tasks (Introduction to the Timer Class)

Source: Internet
Author: User

Set up timed tasks (Introduction to the Timer Class)

In many of our projects, we need to use timed tasks, so we would like to take this blog to introduce a timed task.

To set a scheduled task, proceed as follows:

  • First new Timer object Timer timer = new timer () ;
  • Then call its schedule method, for example:timer.schedule (New Task (), 5 * 1000,5*1000);

Believe that the above code we will not be unfamiliar, even if we did not write, but we must have seen.

Note: The schedule method mentioned in the second step above has four overloaded methods. Here is a brief introduction

  • public void Schedule (TimerTask task,long delay)
  • public void Schedule (TimerTask task,long Delay,long period)
  • public void Schedule (TimerTask task,date time)
  • public void Schedule (TimerTask task,date Firsttime,long period)

1) public void schedule(TimerTask task,long delay)
The first parameter in the formal parameter in the above function (TimerTask Task) is the task we are going to perform. This is an TimerTask object, which is exactly the object of a class that implements TimerTask, because TimerTask is an abstract class. Our custom mytask must implement the TimerTask method run, and the task we want to perform is within this run method. For example: We have the following MyTask class inheriting TimerTask, the task is simply an output

import java.util.TimerTask;publicclass MyTask extends TimerTask {   publicvoidrun()  {    System.out.println("我们的定时任务执行了");  }}

The second parameter in the above function (long delay) indicates how long the delay is to begin execution; it needs to be noted that the value of this long is the number of milliseconds, then 5*1000 represents a delay of 5 seconds
For example, the following code, which means that the next program starts after 5 seconds delay after the execution of the task MyTask

import java.util.Timer;publicclass MyTimeTask {   publicstaticvoidmain(String[] args){      new Timer();       timer.schedule(new51000);    }}

2) public void schedule(TimerTask task,long delay,long period)
Sometimes we need to perform some kind of task repeatedly, that is, every once in a while,

  • Some time ago in our project we have encountered, the situation is as follows
    We need to put the data collected by the ECG acquisition device in real-time to the app on our mobile phone to display it. So we need a timed task to repeatedly read the data in the cache.

Then the above overloaded function can help us solve this problem. The first and second parameters of the above function are exactly the same as described earlier, and the third parameter represents the interval between the repeated executions of our task, that is, our task executes every period time.

For example: If we want our task to be executed every 5 seconds, the third parameter is written like this: 5*1000;
Just like the line of code we wrote at the beginning of the post.
Timer.schedule (New Task (), 5 * 1000,5*1000)means that when our program starts, it is delayed for 5 seconds and then executes the task we specified, and then repeats every 5 seconds. Oh, no.

The above two are basically the most commonly used, but when we want to take one of our tasks (such as birthday blessing) in the 2015年6月24日0时0分 execution, the above two functions will not be able to complete, because we do not know when the program is started, we can not determine how long it will take to reach our designated time,

But we also have two other functions, which are described below

3) public void schedule(TimerTask task,Date time)
One of the above parameters is the task we need to perform, the second parameter is the date object that we need to specify the specific time, for example: we want to send our birthday wish on June 24, 2015 0:0, we set it directly to the Date object.
It is necessary to note that there are two things that happen when we use the above function, as follows:

  • First: The program starts at the time we set, for example: June 1, 2015, then our program executes the time we specify to perform the tasks we specify.
  • The second type: The program start time after we set the time, for example: July 1, 2015 0:0, when our program just started, the task we specified is executed immediately.

4) Schedule The last overloaded method is

publicvoidschedule(TimerTask task,Date firstTime,long period)

Compared to the third function, more than a repetition of the task interval, similar to the second function, which is no longer described here, I believe through the previous introduction, the function of the various parameters are better understood.

Summarize
  • Timing tasks are generally relatively simple, as long as you understand the schedule four overloaded functions, and the meaning of each parameter of each overloaded function, then you can correctly use the

Set up timed tasks (Introduction to the Timer Class)

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.