package traditional; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class traditionalTiTist { private static int count=0; public static void main(String[] args) { // new Timer().schedule(new TimerTask() { // // @Override // public void run() { // System.out.println("炸弹"); // // } // }, 1000,3000); class MyTimerTask extends TimerTask{ @Override public void run() { count=(count+1)%2; System.out.println("炸弹1"); new Timer().schedule(new MyTimerTask(), 2000+2000*count); } } new Timer().schedule(new MyTimerTask(), 3000); while (true) { System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } } } }
运行结果: