Timer is a commonly used tool class used to execute tasks based on time plans. This article introduces the usage of timer in java, involving the knowledge of javatimer, if you are interested in this article, you can refer to this article to check whether the file is updated on a regular basis. Timer is used here.
It is easy to use. just create a new timer and write a subclass of timertask.
The Code is as follows:
Package comz. autoupdatefile; import java. util. timer; import java. util. timerTask; public class M {public static void main (String [] args) {// TODO todo. generated by zoer Timer timer = new Timer (); timer. schedule (new MyTask (), 1000,200 0);} class MyTask extends TimerTask {@ Override public void run () {System. out. println ("dddd ");}}
In this way, you can start executing mytask every two seconds after one second.
Of course, the timer function can also construct a thread by itself, and then use sleep in the thread to simulate and stop for a period of time, and then execute an action.
In fact, you can see the source code of timertask immediately. timertask implements the runnable interface. That is to say, it is done through a thread to execute an operation at intervals through timer.