Java Multithreaded Programming Essentials (II)------CHUANGXIN01 __ Algorithm

Source: Internet
Author: User
Tags terminates

4. Use of timer and timer Task

A timer is a timer tool that is used to schedule a specified task in a background thread, which can be executed once or executed on a regular basis. Each timer object corresponds to a background thread that executes all timer tasks sequentially. If a timer task is completed too long, it will "monopolize" the task of the timer to execute the thread, potentially delaying the execution of subsequent tasks. When the last reference to a timer object completes and all outstanding tasks have been completed, the timer's task execution thread terminates gracefully (and becomes the object of garbage collection). TimerTask is an abstract class that implements the Runable interface, and its subclasses represent a task that can be scheduled by a timer.

1 A simple demo, so that everyone on the timer, the use of TimerTask has a perceptual understanding.

2 timer and TimerTask Common API function description

This emphasizes the difference between the schedule and scheduleatfixedrate of the timer class. The difference between schedule and scheduleatfixedrate is that schedule executes at a fixed relative time interval, and if one execution is delayed, the execution time of the subsequent execution is relatively delayed. While Scheduleatfixedrate is performed at an absolute time interval, and if one execution is delayed, the delay of its subsequent execution will be shortened (Scheduleatfixedrate will take the time already passed as a cycle). Schedule focus on the stability of the time interval, while scheduleatfixedrate focus on the stability of the frequency of execution.

3) The termination of the timer

By default, as long as a program's timer thread is running, the program will remain running. Of course, you can terminate a timer thread in the following four ways:

A the Cancle method that invokes the timer. You can call this method from anywhere in the program, even in a timer task's Run method;

b Let the timer thread become a daemon thread (you can use the new timer (TRUE) when you create the timer) so that it automatically terminates when the program has only daemon threads;

c When all tasks related to a timer have been executed, delete all references to this timer object (set to null) so that the timer thread terminates;

d) Call the System.exit method to terminate the entire program (all threads).

Summary: Timer and timertask can be simply understood as timer timer in triggering TimerTask task calls, usually using schedule and Scheduleatfixedrate methods to invoke TimerTask tasks, Cancle to terminate the task invocation. The timer is simple to use and more suitable for lightweight timer functions, but it can be implemented in other ways (as Javadoc said, "Timer does not offer real-time guarantees:it schedules Tasks using the object.wait (long) method).

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.