日期:2014-05-20 浏览次数:20748 次
Timer t = new Timer(); TimerTask tt = new TimerTask() { private int time = 0; @Override public void run() { System.out.println(++time); if (time >= 5) { this.cancel(); } } }; t.schedule(tt, 0, 1000); // sleep for 3 seconds try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } tt.cancel(); TimerTask tt1 = new TimerTask() { private int time = 0; @Override public void run() { System.out.println(++time); if (time >= 5) { this.cancel(); } } }; t.schedule(tt1, 0,1000);