标签:
区分二者的最好方法是 timer的启示时间设置一个过去的时间T,scheduleAtFixedRate会把从T到现在的任务全部执行,schedule只会从现在开始计时执行并任务。
public class HelloMain { private static int count = 0; public static void main(String[] args) { Date date = new Date(System.currentTimeMillis() - 3000); Timer timer = new Timer(); System.out.println("timer schedule before"); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { count++; System.out.println(count + " timer task run " + Calendar.getInstance().getTime()); } }, date, 1000); System.out.println("timer schedule after"); try { Thread.sleep(6000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("timer cancel before"); timer.cancel(); System.out.println("timer cancel after"); } }
schedule vs scheduleAtFixedRate
标签:
原文地址:http://blog.csdn.net/torvalbill/article/details/46419011