package com.thread.timer; import java.util.Date; import java.util.Timer; import java.util.TimerTask; /** * 间隔2执行一次再隔4秒执行一次 * * */ public class TraditionalTimerTest { public static void main(String[] args) throws InterruptedException { Timer timer = new Timer(); long cur = System.currentTimeMillis(); timer.schedule(new MyTimerTask(), 2000); while (true) { System.out.println(new Date().getSeconds()); Thread.sleep(1000); } } } class MyTimerTask extends TimerTask { private static int count = 0; @Override public void run() { count = (count + 1) % 2; // TODO Auto-generated method stub System.out.println("bombing!!"); new Timer().schedule(new MyTimerTask(), 2000+2000*count); } }