Java Timer Timer Management class

Source: Internet
Author: User
Tags terminates java util

1.java Timer class, Timer class. The Start Execution Timer task method is Timer.schedule (new remindtask (), seconds*1000), and the two parameters are TimerTask subclasses, which perform the actions performed by the timed action, and the latter parameter is the Hao second, which represents how often the interval is Executed.
2.TimerTask class, java.util.TimerTask, The operation of the specific timing. There is a run method, just like the thread Run method (which is the thread).
5.Timer is a timer tool that is used to schedule a specified task in a background thread. It can schedule a task to execute once or repeatedly.
TimerTask an abstract class whose subclasses represent a task that can be scheduled by the Timer.
6.Timer can perform tasks more than once, or you can have the timer execute only once at a fixed time. All are Timer.sehedule () methods, which are overloaded methods that can pass multiple Arguments.
Timer.scheduleatfixedrate (timertask, firsttime, period), executed multiple times in a fixed interval.
Timer.schedule (timertask, firsttime), executed once at a fixed time.
The relationship between 7.Timer and timertask, the timer is the TimerTask management tool, a timer can correspond to multiple timertask, start multiple timertask, and manage their execution.
Since it is a timer that can bind multiple timertask, we can encapsulate a singleton class in the code, encapsulating the timer IN. Then use this timer to dispatch and manage all the TimerTask
Timerservice.starttimertask (new alarmtimertask (), 3 * 1000, 1 * 1000);
Timerservice.starttimertask (new AlarmTimerTask2 (), 3 * 1000, 1 * 1000);
There is no difference between the 8.static method and the normal method, the code inside the static method can be arbitrarily new, provided that the code is RUNNING. Do not execute this static method in the static code BLOCK. Other static methods that are called during run time can contain normal code.
Such as: public static Timerservice getinstance () {
Timer timer = new Timer ();
Return instance;
}
2.java util package is a good package, there are logging log tool class, as well as the properties configuration file, timer class. So it is really a toolkit, face a lot of tool class.
Java.util,logging
Java.util.properties
Java.util.timer

4. A simple routine:
Import java.util.Timer;
Import java.util.TimerTask;

/**
* Simple Demo this uses Java.util.Timer to schedule a task to execute
* Once 5 seconds have passed.
*/

public class Reminder {
Timer timer;

Public Reminder (int Seconds) {
Timer = new Timer ();
Timer.schedule (new remindtask (), seconds*1000);
}

Class Remindtask extends TimerTask {
public void Run () {
System.out.println ("time ' s up!");
Timer.cancel (); Terminate the timer thread
}
}

public static void main (String Args[]) {
System.out.println ("about to schedule task.");
New Reminder (5);
System.out.println ("Task scheduled.");
}
}

5. There is also a way to specify the execution time of a task, as the following example specifies that the task is performed at 11:01 P.m.:
Get the Date corresponding to 11:01:00 pm TODAY.
Calendar Calendar = Calendar.getinstance ();
Calendar.set (calendar.hour_of_day, 23);
Calendar.set (calendar.minute, 1);
Calendar.set (calendar.second, 0);
Date time = Calendar.gettime ();

Timer = new Timer ();
Timer.schedule (new remindtask (), time);
2. Terminating the timer thread
By default, The program stays running as long as a program's timer thread is Running. of course, You can terminate a timer thread in the following four ways:


Call the Timer's cancle method. You can call this method from anywhere in the program, even in a timer Task's run Method.
Make the timer thread a daemon thread (you can use the new timer (true) to reach this point when you create the timer), so that when the program has only daemon threads, it will automatically terminate the Run.
When all the tasks related to the timer have been executed, Delete all references to this timer object (null) so that the timer thread Terminates.

The second approach: make the timer thread a daemon thread (you can use the new timer (true) to reach this point when you create the timer), so that when the program has only daemon threads, it will automatically terminate the Run.
If it is set to daemon, then when the main thread ends, the program is left with the timer daemon thread, so the program does not wait for the timer thread to execute the task to Terminate.
When all the tasks related to the timer have been executed, Delete all references to this timer object (null) so that the timer thread Terminates.


3. Note: A timer corresponds to only one thread, not multiple threads, and the timer itself is a thread. Even if the interval is performed multiple times, it is the same thread. Multiple timertask that are bound by a timer are run on a thread thread the Timer. The Java code can be tested, the timer starts two timertask, in each timertask output the name of the current thread thread.currentThread.getName (), it must be the Same. Multiple timertask are run on a timer line thread, and this thread name is called Timer-x


Two. The Java.util.Timer timer is actually a thread that is scheduled to be owned by the Timertasks.


1 What does anonymous inner class mean? New object, the class that writes directly to implement the code, is to create an anonymous class
TimerTask task = new TimerTask () {
public void Run () {
...//each time the code needs to be executed is put into this Area.
}
};

Java Timer Timer Management 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.