传统定时器 发表于 2018-02-22 | 分类于 juc 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950package cn.itcast.heima2;import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class TraditionalTimerTest { private static int count = 0; public static void main(String[] args) {/* new Timer().schedule(new TimerTask() {//todo 第一次炸是10s后,以后每隔3s炸一次 @Override public void run() { System.out.println("bombing!"); } }, 10000,3000);*/ class MyTimerTask extends TimerTask{ @Override public void run() { count = (count+1)%2; System.out.println("bombing!"); new Timer().schedule(/*new TimerTask() { @Override public void run() { System.out.println("bombing!"); } }*/new MyTimerTask(),2000+2000*count); } } new Timer().schedule(new MyTimerTask(), 2000);//间隔2s和4s炸 while(true){ System.out.println(new Date().getSeconds()); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}