- 交互实现两秒和四秒显示爆炸
public class Timer01 {
private static int count=0;
public static void main(String[] args) {
//定时器实例一次只能实现一次操作,所以需要写个继承于TimerTask的类来不断实例
class MyTimer extends TimerTask{
@Override
public void run() {
count=(count+1)%2;
System.out.println("爆炸");
new Timer().schedule(new MyTimer(),2000+2000*count);
}
}
new Timer().schedule(new MyTimer(), 2000);
//通过线程休眠显示秒数
while (true) {
System.out.println(new Date().getSeconds());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}