In the JDK library, the timer class is primarily responsible for the ability to schedule tasks, that is, to start a task at a specified time. The main function of the timer class is to set up a scheduled task, but the TimerTask class is the one that encapsulates the task, and the code that executes the scheduled task is placed in the TimerTask subclass. 1.schedule (timertask task,date timer);
MyTask
The public class MyTask extends timertask{@Override the public
Void Run () {
System.out.println ("Task executed, the time is:" + new Date ());
try {
//No need to sleep, write this to verify the things behind
thread.sleep (1000);
} catch (Interruptedexception e) {
E.printstacktrace ();}}
Test1
public class Test1 {public
static void Main (string[] args) {
System.out.println ("Current time is:" + new Date ());
Calendar calendar = Calendar.getinstance ();
Calendar.add (calendar.second,10);
Date date = Calendar.gettime ();
MyTask MyTask = new MyTask ();
Timer timer = new timer ();
10 seconds to execute, data is execution time
timer.schedule (mytask,date);
}
After execution, you can see that the program is still running. You can see that the timer's construction method opens a thread, not a daemon thread, so it will always run
The characteristics of a timer
1. The time to perform the task is later than the current time-for future execution
2. Plan time earlier than the current time-run ahead
The 3.Timer allows multiple timer tasks, which are executed sequentially in a queue, so that the execution time may be inconsistent, for example, when the previous task consumes a long time, and then the task runs later
Mytaska
public class Mytaska extends timertask{
@Override public
void Run () {
try {
System.out.println ("A start Time: "+ new Date ());
Thread.Sleep (20000);
System.out.println ("A End Time:" + new Date ());
} catch (Interruptedexception e) {
e.printstacktrace ();}}}
Mytaskb
public class Mytaskb extends TimerTask {
@Override
the public void Run () {
System.out.println ("B Time of Start:" + New Date ());
System.out.println (Time of "B end:" + new Date ());
}
Test2
public class Test2 {public
static void Main (string[] args) {
System.out.println ("Current time is:" + new Date ());
Calendar calendar1 = Calendar.getinstance ();
Date Rundatea = Calendar1.gettime ();
System.out.println ("A Planned execution time is:" + Rundatea);
Calendar calendar2 = Calendar.getinstance ();
Calendar2.add (calendar.second,10);
Date rundateb = Calendar2.gettime ();
SYSTEM.OUT.PRINTLN ("B Plan execution Time is:" + rundateb);
Mytaska Mytaska = new Mytaska ();
MYTASKB MYTASKB = new MYTASKB ();
Timer timer = new timer ();
Timer.schedule (Mytaska,rundatea);
Timer.schedule (MYTASKB,RUNDATEB);
}
Run results
The Plan B, which was supposed to be executed in 10 seconds, had to be 2.schedule (TimerTask task,date Firsttime,long period) after 20 seconds because a plan was executed for 20 seconds;
Performs a task wirelessly, after a specified date, at a specified interval period. Features are as follows
1. Schedule time later than the current time-for future execution
2. Plan time earlier than the current time-run ahead
3. Because a task is executing too long, resulting in longer than the cycle time long, the task is delayed execution
The purpose of the 4.cancel () method is to remove itself from the task queue and remove the loop later
5.Timer Cancel () clears all tasks in the queue 3.schedule (timertask task,long delay);
To execute the current time of the method as the reference time, based on the delay of the specified number of milliseconds after a timertask task 3.schedule (timertask taks,long delay,long period);
Delay specifies a good second to execute and loop through the 4.scheduleAtFixedRate (TimerTask task,date Firsttimer,long period) at a certain interval
Like schedule are executed in order, so do not consider the situation is not thread-safe, the difference is that there is no catch, if the start time in n seconds ago, then Scheduleatfixedrate will ignore the period, continuous execution, until the n seconds to catch up