First, let’s take a look at the timer that comes with jdk:
A tool that threads use to arrange tasks that will be executed in background threads later. Tasks can be executed once, or they can be executed regularly. Corresponding to each Timer object is a single background thread that performs all timer tasks sequentially. The timer task should be completed quickly. If a timer task is completed too long, it will "exclusively" the timer's task execution thread. Therefore, this may delay the execution of subsequent tasks, which may be "piled together" and can only be executed in rapid succession only when the above-mentioned unfriendly tasks are finally completed.
schedule(TimerTasktask,long delay) Schedule the execution of the specified task after the specified delay.
schedule(TimerTask task,Datetime) Schedule the execution of the specified task at the specified time. If this time has passed, schedule the task to be executed immediately.
schedule(TimerTasktask, long delay, long period) Schedule the specified task for repeated fixed delay execution from the specified delay. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent execution will also be delayed
schedule(TimerTask task,DatefirstTime,long period) Schedule the specified task for repeated fixed delay execution at the specified time. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent execution will also be delayed.
package test; import ; import ; import ; import ; import ; /** * jdk comes with timer * * @author LIUTIE * */ public class JDKTimer { public static void main(String[] args) throws ParseException { //Date format tool final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Timer timer = new Timer(); // Execute the timer after 10s, only once ((new Date())); ("the timer one will be executed after 10 seconds..."); long milliseconds = 10 * 1000; (new TimerTask() { @Override public void run() { ((new Date())); ("the timer one has finished execution"); } }, milliseconds); //Execute the timer after 12 seconds, and execute it every 1s ((new Date())); ("the timer two will be executed after 12 seconds..."); //Delay time after startup long afterSs = 12 * 1000; //Execution cycle long intervalSs1 = 1 * 1000; (new TimerTask() { // Execute counter int i = 0; @Override public void run() { ((new Date())); ("the timer two has execution " + (++i) + " timers"); // Turn off the timer after 10 executions if (i == 10) { (); } } }, afterSs, intervalSs1); // Execute the timer at a specified time, only once ((new Date())); ("the timer three will be executed at 2017-06-27 21:47:00..."); Date date = ("2017-06-27 21:47:00"); (new TimerTask() { @Override public void run() { ((new Date())); ("the timer three has finished execution"); } }, date); // Periodic execution starts from the specified time ((new Date())); ("the timer four will be executed at 2017-06-27 21:48:00..."); // Execution interval period long intervalSs = 1 * 1000; // Start execution time Date beginTime = ("2017-06-27 21:48:00"); (new TimerTask() { // Execute counter int i = 0; @Override public void run() { ((new Date())); ("the timer four has execution " + (++i) + " timers"); // Turn off the timer after 10 executions if (i == 10) { (); } } }, beginTime, intervalSs); } }
Execution results
2017-06-27 21:46:24the timer one will be executed after 10 seconds... 2017-06-27 21:46:24the timer two will be executed after 12 seconds... 2017-06-27 21:46:24the timer three will be executed at 2017-06-27 21:47:00... 2017-06-27 21:46:24the timer four will be executed at 2017-06-27 21:48:00... 2017-06-27 21:46:34the timer one has finished execution 2017-06-27 21:46:36the timer two has execution 1 timers 2017-06-27 21:46:37the timer two has execution 2 timers 2017-06-27 21:46:38the timer two has execution 3 timers 2017-06-27 21:46:39the timer two has execution 4 timers 2017-06-27 21:46:40the timer two has execution 5 timers 2017-06-27 21:46:41the timer two has execution 6 timers 2017-06-27 21:46:42the timer two has execution 7 timers 2017-06-27 21:46:43the timer two has execution 8 timers 2017-06-27 21:46:44the timer two has execution 9 timers 2017-06-27 21:46:45the timer two has execution 10 timers 2017-06-27 21:47:00the timer three has finished execution 2017-06-27 21:48:00the timer four has execution 1 timers 2017-06-27 21:48:01the timer four has execution 2 timers 2017-06-27 21:48:02the timer four has execution 3 timers 2017-06-27 21:48:03the timer four has execution 4 timers 2017-06-27 21:48:04the timer four has execution 5 timers 2017-06-27 21:48:05the timer four has execution 6 timers 2017-06-27 21:48:06the timer four has execution 7 timers 2017-06-27 21:48:07the timer four has execution 8 timers 2017-06-27 21:48:08the timer four has execution 9 timers 2017-06-27 21:48:09the timer four has execution 10 timers
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.