scheduleAtFixedRate和scheduleWithFixedDelay的区别

二者都是每隔多长时间执行一次任务,那么有什么区别呢?

  • scheduleAtFixedRate:真正的间隔时间,是由任务的执行时间和设置的间隔时间长短来决定的,谁长取谁。也就是说,以上一个任务开始的时间计时,period时间过去后,检测上一个任务是否执行完毕,如果上一个任务执行完毕,则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行。
  • scheduleWithFixedDelay:真正的间隔时间,是任务的执行时间加上设置的间隔时间。也就是说,是以上一个任务结束时开始计时,period时间过去后,立即执行。
package timers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduleExecutorServiceTest
{

    public static void main(String[] args) {
        ScheduleExecutorServiceTest test = new ScheduleExecutorServiceTest();
        test.testWithFixedDelay();
//        test.testAtFixedRate();
    }
    
    private ScheduledExecutorService executor;
    
    public ScheduleExecutorServiceTest() {
        executor = Executors.newScheduledThreadPool(4);
    }
    
    /**
     * 执行时间和间隔时间,谁长取谁。
     * @description           			 
     * @author tangcy 
     * @datetime 2019年5月31日 下午2:10:31
     * 
     *  2019-05-31 14:16:09线程:pool-1-thread-1:Sleeping158ms     执行时间需要 0.158s,少于5秒,则过5秒执行一次
     *  2019-05-31 14:16:14线程:pool-1-thread-1:Sleeping2643ms    执行时间需要 2.643s,少于5秒,则过5秒执行一次
     *  2019-05-31 14:16:19线程:pool-1-thread-2:Sleeping7613ms    执行时间需要 7.613s,大于5秒,则过7秒执行一次
     *  2019-05-31 14:16:26线程:pool-1-thread-1:Sleeping8256ms
     *  2019-05-31 14:16:35线程:pool-1-thread-1:Sleeping8565ms
     *  2019-05-31 14:16:43线程:pool-1-thread-1:Sleeping9853ms
     * 
     *  通过观察得到,执行时间和间隔时间,谁长取谁。
     * 
     */
    public void testAtFixedRate() {
        executor.scheduleAtFixedRate(new Runnable() {
            
            public void run() {
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                long time = (long) (Math.random() * 10000);
                // 输出线程的名字和使用目标对象及休眠的时间
                System.out.println(sf.format(new Date())+"线程:"+Thread.currentThread().getName()+":Sleeping"+time+"ms");
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    
                }
            }
        }, 0, 5000, TimeUnit.MILLISECONDS);
    }
    
    /**
     * scheduleWithFixedDelay
     *      从字面意义上可以理解为就是以固定延迟(时间)来执行线程任务,
     *      每次都要把任务执行完成后再延迟固定时间后再执行下一次。
     * @description           			 
     * @author tangcy 
     * @datetime 2019年5月31日 下午2:02:02
     */
    public void testWithFixedDelay() {
        executor.scheduleWithFixedDelay(new Runnable() {
            
            public void run() {
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                long time = (long) (Math.random() * 10000);
                // 输出线程的名字和使用目标对象及休眠的时间
                System.out.println(sf.format(new Date())+"线程:"+Thread.currentThread().getName()+":Sleeping"+time+"ms");
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    
                }
            }
        }, 0, 3000, TimeUnit.MILLISECONDS);
    }
}
  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值