The timer is a timer tool provided in the JDK that, when used, opens a separate thread outside the main thread to perform the specified task, which can be executed once or repeatedly. TimerTask is an abstract class that implements the Runable interface, representing a task that can be performed by a timer.
Simply try it:
public class Timertasktest extends timertask{private int id;public timertasktest (int id) {this.id = ID;} @Overridepublic void Run () {System.out.println ("Thread:" +id+ "Running ...");}}
public class Timertest {public static void main (string[] args) {Timer ti = new timer (); Ti.schedule (new Timertasktest (1), 5 000);//5 seconds after execution of Ti.schedule (new Timertasktest (2), 1000, 3000);//1 seconds after execution, followed by 3-second cycle execution System.out.println ("Mian end.");}
If you terminate the timer, you can call the timer's Cancle method or call the System.exit method.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Timer and TimerTask