定时器
package com.gyj.student;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
-
定时器
-
@author guoyajie
*/
public class Test3 {
public static void main(String[] args) {
//task任务 什么时候开始 周期
// new Timer().schedule(new TimerTask() {
// @Override
// public void run() {
// System.out.println(“bombing!”);
// }
// }, 1000,3000);class MyTimerTask extends TimerTask{ @Override public void run() { System.out.println("bombing!"); new Timer().schedule(new MyTimerTask(), 2000); } } new Timer().schedule(new MyTimerTask(), 2000); while(true){ System.out.println("当前是多少秒" + new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
}
}