码迷,mamicode.com
首页 > 其他好文 > 详细

schedule vs scheduleAtFixedRate

时间:2015-06-08 23:23:35      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

区分二者的最好方法是 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!