Java core programming -- thread timer (TimerTask) (3), javatimertask
1. TimerTask;
TimerTask is a special thread that schedules one or more tasks for Timer. It implements the Runable interface and is an abstract class. He does not implement the run () method by himself, and must implement it by subclass. Unlike a thread, a Timer task is started at a scheduled time, so you cannot use the start () method directly. Instead, you need a special Timer class to start it.
2. Timer
Timer is a tool used to schedule tasks executed in background threads. It can schedule tasks to be executed once or multiple times. Each Timer object corresponds to a backend thread for sequential execution of all Timer tasks.
3. Specific applications
Package se. thread; import java. util. date; import java. util. timer; import java. util. timerTask; public class ThreadTimerTest extends TimerTask {@ Override public void run () {System. out. println (new Date ();} public static void main (String [] args) {ThreadTimerTest threadTimerTest = new ThreadTimerTest (); Timer timer = new Timer (); timer. schedule (threadTimerTest, new Date (), 3000 );}}