The Java2 development package provides a very useful thread function: an advance arrangement in advance to perform a task Timer, Timertask. You can use these classes to create a background process, allowing it to be executed after waiting for a specified time, or makes it executed for a while. You can also use Thread to do it, but use Timer and Timertask clearly save trouble. TIMERTASK implements the runnable interface, and the task to be executed is placed in Run (). Timer is a class that schedules Timertask thiss. When applied, you first created a TIMERTASK object, then arrange the method of Timer class with a Timer class object: 1.Timer () runs the background process thread when running Task 2.Timer (Boolean) True in a regular manner. . As long as the remaining procedures are running, the background process thread will execute. 3. Public Void Cancel () Terminates the function of Timer, but does not have an impact on the task being executed. It will not be able to use its task again after performing the CANCEL method. 4. Public Void Schedule (Timertask Task, Date Time) Task is scheduled to execute the time specified by the time, if the time is in the past, the task is immediately implemented. 5.public void schedule (Timertask Task, Date FirstTime, long period) Task is scheduled to execute the time specified by the time, execute it repeatedly after execution, and executes every period (millisecond). Since the specified time interval does not guarantee that the clock is accurate, the party is
Method is best for places from short-term staying frequencies. 6.public void schedule (Timertask Task, long delay) Task is scheduled to be executed after DELAY (mi). 7.public void schedule (Timertask Task, Long DELAY, Long Period) Task is scheduled after the time specified by DELAY (mi). After execution, it will be executed repeatedly after Period (mi). 8.public void scheduleatFixedrate (Timertask Task, Date First Task is scheduled to perform the time specified in Firsttime. After execution, it will be executed repeatedly after Period (mi). Every time the time is repeated, the box is performed instead of being related to the previous execution.
. Therefore, the total speed is performed is fixed. 9.public void scheduleatfixedrate (Timertask Task, long delay) Task is scheduled to be executed after the time specified in Delay (milliseconds). After execution, it will be executed repeatedly after Period (mi). Each time the time is repeated, the box is performed instead of and the previous execution.
The line is related. Therefore, the total speed is performed is fixed. The main method of Timertask: 1.Public Boolean Cancel () Terminates the task to run. If the time is required to be executed, if it is executed, it will be loop again if it is executed. Will not be executed if it has not been executed or in a stop period
2.Public Abstract Void Run () 3.Public long scheduledexecutionTime () Returns the time scheduled for the last execution task. Generally determining whether today's implementation is sufficient enough to prove that the planned activity is justified: public void Run () {if (System.currentTimeMillis () - ScheduledExecutionTime () - 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 (interruptexception e) {} mytask1.end (); mytask2.end (); // End Timer} at the end of the program
Public void start (int delay, int interface) {mytimer.schedule (this, delay * 1000, internal * 1000); // utilize Timer.Schedule method} public void end () {MyTimer.cancel ();}}
Output: First Tasksecond Tasksecond Tasksecond Taskfirst Tasksecond Tasksecond Tasksecond Task