Java Timer Task Scheduling tool

Source: Internet
Author: User

First, what is scheduled task scheduling

A task that is automatically performed based on a given point in time, a given time interval, or a given number of times.

Second, the common use of Java Timer Task scheduling tool:

    • Timer
    • Quartz

2.1 Differences between the two:

    • The timer originates from Jdk,quartz and requires additional jar packages.
    • Timer function is few, easy to use, can solve many common problems. Quartz is a powerful, hassle-capable solution that solves almost any problem.
    • The timer executes the timer task from the bottom of the thread. Quartz The bottom layer performs timed tasks through multiple thread pools.

2.2Timer Introduction

Definition: There is only one background thread for multiple business threads , the timing of the fixed frequency of the transfer .

Build: The timer has four components:

    • Timer: The main class of Timer task scheduling tool.
    • TimerTask: Timed task with Run method
    • TimerThread: The background responsible for executing the timed thread
    • Taskqueue: Timed Task queue

The most important part of this is the timer and TimerTask.

How to use the timer:

/*** Define TimerTask*/classMytimertaskextendstimertask{PrivateString name;  PublicMytimertask (String name) { This. Name =name; }         Public voidsetName (String name) { This. Name =name; }         PublicString GetName () {returnname; } @Override Public voidrun () {System.out.println ("Current exec Name:" +NewDate () + "" +name); }}
 Public classMain { Public Static voidMain (string[] args)throwsparseexception {simpledateformat sdf=NewSimpleDateFormat ("Yyyy/mm/dd HH:mm:ss"); //1. Create a timerTimer timer =NewTimer (); //2. Create a TimerTaskTimerTask task =NewMytimertask ("First"); //Set Call Frequency//timer.schedule (Task, Sdf.parse ("2018/3/24 10:42:30"));//The date time is executed at the execution date and is executed immediately if the time has elapsed. Do not repeat//timer.schedule (task, 1000L);//Long Delay executes after the specified number of milliseconds is delayed. Do not repeat//timer.schedule (Task, Sdf.parse ("2018/3/24 10:42:30"), 1000L);//Date Firsttime, long period specifies the execution time of the first time, if the time has elapsed, it is executed immediately and then executed again after the specified number of milliseconds//timer.schedule (Task, 2000L, 1000L);//long delay, long period the first execution of latency delay milliseconds, followed by repeated execution every period second. //timer.scheduleatfixedrate (Task, Sdf.parse ("2018/3/24 10:50:10"), 10000L);//Specifies the first execution time, then executes again after the specified number of milliseconds, and if the time has elapsed, the threads in this period are replenished, and there is a possibility of concurrencyTimer.scheduleatfixedrate (Task, 1000L, 10000L); }}

In the Timer class, the method names that perform timed tasks are mainly divided into scheduleatfixedrate and schedule. The difference between the two:

1. The schedule method does not supplement the missed task. Scheduleatfixtedrate will supplement the missed tasks.

For example: If there is a scheduled task 12:00:00, then repeat every 2 seconds. But the current time is already 12:10:00. Schedule immediately executes the task once, then repeats every two seconds, Scheduleatfixedrate immediately executes 10*60/2 to the task, and then begins execution at 12:00:00 intervals.

2. The time of schedule recurrence starts from the last task execution end time. Scheduleatfixedrate the time that the recurrence was executed starts from the time the last task started executing.

For example, it takes 6 seconds for a scheduled task to execute, and the interval between the two tasks is 5 seconds. Then schedule the first execution time 12:00:00, the second execution time 12:00:11. Scheduleatfixedrate first Execution time 12:00:00, second execution time 12:00:05.

In summary, when using scheduleatfixedrate, you need to consider the problem of thread concurrency.

Other important methods:

Timertask.cancel (): Cancels the current scheduled task schedule after executing a local task.

Timertask.schedule (): Returns the time that the most recent execution of this task was scheduled.

Timer.cancel (): Terminates the current timer and cancels all pending tasks within the timer.

Timer.purge (): Removes all canceled tasks from the timer and returns the number of cancellations.

Defects in 2.3Timer

    • Multithreading concurrency is not supported because there is only one background thread. That is, if there are multiple scheduled tasks in a single timer that start at the same time, they do not start at the same time.
    • When one of the tasks throws a run-time exception, the entire timer stops all tasks and throws an exception.

Java Timer Task scheduling tool

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.