|
The Java 2 Development Kit provides a very useful thread function: Schedule the execution of a task timer and timertask at a certain time in advance. You can use these classes to create backend processes so that they can be executed after a specified period of time, or they can be executed for a period of time. You can also use thread to complete the task, but using timer and timertask obviously saves a lot of trouble.
Timertask implements the runnable interface, and the tasks to be executed are placed in run. Timer is similar to timertask. Create a timertask object first, and then schedule it to be executed with a timer class object.
The timer class includes the following methods:
1. Timer ()
Run the task in the conventional mode
2. Timer (Boolean)
If the value is true, the background process thread is used. As long as the remaining programs are recorded and run, the background process thread will execute.
3. Public void cancel ()
Terminate the execution of the timer function, but it does not affect the ongoing tasks. After the cancel method is executed, you cannot use it to allocate tasks.
4. Public void schedule (timertask task, date time)
The task is scheduled to run at the time specified by time. If the time is past, the task is executed immediately.
5. Public void schedule (timertask task, date firsttime, long period)
The task is scheduled to be executed at the time specified by time. After execution, the task is executed repeatedly at intervals of period (milliseconds. Because the specified time interval does not guarantee the same precision as the clock
It is more important to keep the frequency accurate in the short term.
6. Public void schedule (timertask task, long delay)
The task is scheduled to be executed after the time specified by delay (MS.
7. Public void schedule (timertask task, long delay, long period)
The task is scheduled to be executed after the time specified by delay (MS. After execution, it will be executed every other period (milliseconds.
8. Public void scheduleatfixedrate (timertask task, date firsttime, long period)
Tasks are scheduled to be executed at the specified time of firsttime. After execution, it will be executed every other period (milliseconds. The first execution of the box at each time of repetition is not related to the previous execution.
. Therefore, the total execution speed is fixed.
9. Public void scheduleatfixedrate (timertask task, long delay, long period)
The task is scheduled to be executed after the time specified by delay (MS. After execution, it will be executed every other period (milliseconds. The first execution of the box rather than the previous execution of each repeated time
. Therefore, the total execution speed is fixed.
Timertask methods:
1. Public Boolean cancel ()
Terminate the execution of a task. If timer requires cyclic execution, if it is in progress, the execution will be completed and then the process will be repeated. If the task is not executed or is in a stopped period, the task is not executed.
Now
2. Public abstract void run ()
3. Public long scheduledexecutiontime ()
Returns the scheduled time for the last task to be executed. It is generally determined whether the current implementation of the task is timely enough to prove that the scheduled activities are justified:
Public void run (){
If (system. currenttimemillis ()-scheduledexecutiontime ()> =
Max_tardiness)
Return; // too late; skip this execution.
// Perform the task
}
4. Protected timertask ()
Example:
Import java. util .*;
Import java. Io .*;
Public class dotask extends timertask {
String index;
Timer mytimer = new timer ();
Public dotask (string index ){
This. Index = index;
}
Public void run (){
System. Out. println (INDEX );
}
Public static void main (string ARGs []) {
Dotask mytask1 = new dotask ("first task ");
Mytask1.start (0, 3 );
Dotask mytask2 = new dotask ("second task ");
Mytask2.start (0, 1 );
Try {
Thread. Sleep (6000 );
}
Catch (interruptedexception e ){
}
Mytask1.end ();
Mytask2.end (); // use cancel () to end the timer when the program ends.
}
Public void start (INT delay, int internal ){
Mytimer. Schedule (this, delay * 1000, internal * 1000); // use the timer. Schedule Method
}
Public void end (){
Mytimer. Cancel ();
}
}
Output:
First task
Second Task
Second Task
Second Task
First task
Second Task
Second Task
Second Task
========================================================== ==============
Timer and timertask usage
Use timertask
Import java. util. timer;
Import java. util. timertask;
Import java. util. date;
/**
* @ Author Vincent
*/
Public class timertest {
Public static void main (string [] ARGs ){
Timer T = new timer ();
// Execute the timertask task in 5 seconds
T. Schedule (New timertask (){
Public void run ()
{System. Out. println ("this is task you do1 ");}
}, 5*1000 );
// Execute the timertask task after the specified time point of date
Date d1 = new date (system. currenttimemillis () + 1000 );
T. Schedule (New timertask (){
Public void run ()
{System. Out. println ("this is task you do2 ");}
}, D1 );
// Execute the timertask task once every one second after the specified time point of date
Date D2 = new date (system. currenttimemillis () + 1000 );
T. Schedule (New timertask (){
Public void run ()
{System. Out. println ("this is task you do3 ");}
}, D2, 1*1000 );
// After 3 seconds, the timertask is executed once every 1 second.
T. Schedule (New timertask (){
Public void run ()
{System. Out. println ("this is task you do4 ");}
}, 3*1000, 1 );
// After 3 seconds, the timertask is executed every 2 seconds.
T. scheduleatfixedrate (New timertask (){
Public void run ()
{System. Out. println ("this is task you do6 ");}
}, 3*1000, 2 );
}
The difference between schedule and scheduleatfixedrate is that if you specify the execution start time before the current system running time, scheduleatfixedrate also takes the past time as the periodical execution, schedule does not calculate the past time.
For example
Simpledateformat ftime = new simpledateformat ("yyyy/mm/dd hh: mm: SS ");
Date d1 = ftime. parse ("14:10:00 ");
T. scheduleatfixedrate (New timertask (){
Public void run ()
{
System. Out. println ("this is task you do6 ");
}
}, D1, 3*60*1000 );
The interval is 3 minutes, and the specified start time is 14:10:00. If I execute this program at 14:17:00, it will be printed three times immediately.
This is task you do6 //
This is task you do6 //
This is task you do6 // 14: 16
Note that the next execution is at rather. That is to say, the timer starts from the specified start time, rather than from the execution time.
However, if the schedule method is used, the interval is 3 minutes and the specified start time is 14:10:00, then the program will be executed immediately at 14:17:00. In addition, the next execution time is, rather than the period from ).
} |