多線程-傳統定時任務
阿新 • • 發佈:2017-12-27
sta alt rup () color test pac exception lee
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; @Overridepublic void run() { count = (count + 1) % 2; // TODO Auto-generated method stub System.out.println("bombing!!"); new Timer().schedule(new MyTimerTask(), 2000+2000*count); } }
多線程-傳統定時任務