Timer的schedule与scheduleAtFixedRate的区别(三)

schedule与scheduleAtFixedRate的区别

两种情况看区别

1.首次计划执行的时间早于当前的时间
- schedule方法

“fixed-delay”;如果第一次执行时间被delay了,随后的执行时间按照==上一次实际执行完成的时间点==进行计算。

demo 代码

package lxd.timer.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 
 * schedule与scheduleAtFixedRate的区别
 * 
 * <pre>
 *  schedule与scheduleAtFixedRate的区别测试
 * </pre>
 * 
 * @author 李晓东
 * 
 * 2017.05.28
 * 
 * @since 1.0
 *
 */
public class DifferenceTest {

    public static void main(String[] args) {
        //规定时间格式
        final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前的具体时间
        Calendar calendar = Calendar.getInstance();
        System.out.println("Current time is: " + sf.format(calendar.getTime()));
        //设置成6秒前的时间,
        calendar.add(Calendar.SECOND, -6);
        Timer timer = new Timer();
        //第一次执行时间为6秒前,之后没隔两秒执行一次
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                //打印当前的计划执行时间
                System.out.println("scheduled execc time is: " 
                        + sf.format(scheduledExecutionTime()));
                System.out.println("Task is being executed!");
            }
        }, calendar.getTime(), 2000);
    }

}

测试结果:

Current time is: 2017-05-28 14:52:49
scheduled execc time is: 2017-05-28 14:52:49
Task is being executed!
scheduled execc time is: 2017-05-28 14:52:51
Task is being executed!
  • scheduleAtFixedRate方法

    ”fixed-rate”;如果第一次执行时间被delay了,随后的执行时间按照==上一次开始的时间点==进行计算,并且为了==赶上进度==会多次执行任务,因此TimeTask中的执行体需要==考虑同步==。


测试demo:
package lxd.timer.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 
 * schedule与scheduleAtFixedRate的区别
 * 
 * <pre>
 *  schedule与scheduleAtFixedRate的区别测试
 * </pre>
 * 
 * @author 李晓东
 * 
 * 2017.05.28
 * 
 * @since 1.0
 *
 */
public class DifferenceTest {

    public static void main(String[] args) {
        //规定时间格式
        final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前的具体时间
        Calendar calendar = Calendar.getInstance();
        System.out.println("Current time is: " + sf.format(calendar.getTime()));
        //设置成6秒前的时间,
        calendar.add(Calendar.SECOND, -6);
        Timer timer = new Timer();
        //第一次执行时间为6秒前,之后没隔两秒执行一次
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                //打印当前的计划执行时间
                System.out.println("scheduled execc time is: " 
                        + sf.format(scheduledExecutionTime()));
                System.out.println("Task is being executed!");
            }
        }, calendar.getTime(), 2000);
    }

}
测试结果:
Current time is: 2017-05-28 14:58:06
scheduled execc time is: 2017-05-28 14:58:00
Task is being executed!
scheduled execc time is: 2017-05-28 14:58:02
Task is being executed!
scheduled execc time is: 2017-05-28 14:58:04
Task is being executed!
scheduled execc time is: 2017-05-28 14:58:06
Task is being executed!
scheduled execc time is: 2017-05-28 14:58:08
Task is being executed!
scheduled execc time is: 2017-05-28 14:58:10
Task is being executed!
会马上执行三次之前的任务。 2.任务执行所需时间超出任务的执行周期间隔
  • schedule方法

    下一次执行时间相对于==上一次实际执行完成的时间点==,因此执行的时间会==不断延后==。


测试demo
package lxd.timer.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 
 * schedule与scheduleAtFixedRate的区别
 * 
 * <pre>
 *  schedule与scheduleAtFixedRate的区别测试
 * </pre>
 * 
 * @author 李晓东
 * 
 * 2017.05.28
 * 
 * @since 1.0
 *
 */
public class DifferenceTest {

    public static void main(String[] args) {
        //规定时间格式
        final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前的具体时间
        Calendar calendar = Calendar.getInstance();
        System.out.println("Current time is: " + sf.format(calendar.getTime()));

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("scheduled execc time is: " 
                        + sf.format(scheduledExecutionTime()));
                System.out.println("Task is being executed!");
            }
        }, calendar.getTime(), 2000);

    }

}
测试结果:
Current time is: 2017-05-28 15:08:21
scheduled execc time is: 2017-05-28 15:08:21
Task is being executed!
scheduled execc time is: 2017-05-28 15:08:24
Task is being executed!
scheduled execc time is: 2017-05-28 15:08:27
Task is being executed!
  • scheduleAtFixedRate

下一次执行时间相对于==上一次开始的时间点==,因此执行时间一般==不会延后==,因此存在==并发性==。

测试demo:

package lxd.timer.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 
 * schedule与scheduleAtFixedRate的区别
 * 
 * <pre>
 *  schedule与scheduleAtFixedRate的区别测试
 * </pre>
 * 
 * @author 李晓东
 * 
 * 2017.05.28
 * 
 * @since 1.0
 *
 */
public class DifferenceTest {

    public static void main(String[] args) {
        //规定时间格式
        final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前的具体时间
        Calendar calendar = Calendar.getInstance();
        System.out.println("Current time is: " + sf.format(calendar.getTime()));

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("scheduled execc time is: " 
                        + sf.format(scheduledExecutionTime()));
                System.out.println("Task is being executed!");
            }
        }, calendar.getTime(), 2000);

    }

}

测试结果:

Current time is: 2017-05-28 15:13:03
scheduled execc time is: 2017-05-28 15:13:03
Task is being executed!
scheduled execc time is: 2017-05-28 15:13:05
Task is being executed!
scheduled execc time is: 2017-05-28 15:13:07
Task is being executed!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值