1.showtime
public class ShowTime extends TimerTask {
@Override
public void run() {
//
System.out.println( LocalDateTime.now());
}
}
2.testtimer
public class TestTimer {
//需求:程序 启动后,延时5秒,然后每一秒输出时间
public static void main(String[] args) {
// //定时器
Timer t = new Timer();
// //写法一: 调用外部类
// // 任务 延时 周期
// t.schedule(new ShowTime(), 5000, 1000);
System.out.println("bye bye");
//写法二:使用匿名内部类
t.schedule(new TimerTask() { //抽象类不能new但如果对他进行实现,就可以new
public void run() {
//
System.out.println("新时间:" + LocalDateTime.now());
}
}, 5000, 1000);
}
}