Timing task scheduling
By Timer and Timetask, we can achieve timing to start a thread.
java.util.Timer
In this implementation, the Timer class action is an alarm clock function, i.e. the timing or trigger a thread at predetermined time intervals. In fact, Timer class itself is to achieve a thread, but that thread is used to implement calls to other threads.
java.util.TimerTask
TimerTask class is an abstract class that implements the Runnable interface, multi-threading capabilities class has.
In this implementation, the class through inheritance TimerTask ability to obtain multi-threaded, multi-threaded code will be required to perform writing within the run method and then by performing the Timer class start threads.
java.util.Timer use
public class TestTimer { public static void main (String [] args) { the Timer T1 = new new the Timer (); // custom timer; to MyTask Task1 = new new to MyTask (); // define the task; t1.schedule (task1,3000) ; // performed for 3 seconds; // t1.schedule (task1,5000,1000); // after 5 seconds executed once every second! // the GregorianCalendar Calendar1 the GregorianCalendar new new = (2010,0,5,14,36,57); // t1.schedule (Task1, calendar1.getTime ()); // the specified execution timing of time; } } classTo MyTask the extends TimerTask { // custom thread class inherits class TimerTask; public void RUN () { for ( int I = 0; I <10; I ++ ) { System.out.println ( "Task. 1:" + I); } } }
The results shown in Figure:
When you run the above program, you can feel there is a significant delay (probably three seconds!) Before output. There are a few ways to try it myself!
In actual use, a Timer can be activated any number of threads TimerTask achieved, but there will be blocked between multiple threads. So if you need a completely separate words among multiple threads, it is best to start a TimerTask achieve a Timer.
===========
@Scheduled comment
https://www.jianshu.com/p/1defb0f22ed1
Springboot integrated asynchronous regular tasks
https://blog.csdn.net/qq_32447301/article/details/84668865
spring boot comes with regular tasks, time expressions representing some of the pit a few weeks. .
https://blog.csdn.net/z1040141848/article/details/96973459
springboot integrated quartz achieve dynamic regular tasks
The actual development, we can use open-source framework quanz, more convenient scheduling the timing of the realization of the task. In fact, the content quanz underlying principle is presented here.
https://blog.csdn.net/z291197968/article/details/82351471