ScheduledExecutorService 的两个方法scheduleAtFixedRate scheduleWithFixedDelay

ScheduledExecutorService  将定时任务与线程池功能结合使用:当任务到达执行时间,将任务交于线程池,由线程池分配线程去执行任务。其中有两个方法容易混淆:scheduleAtFixedRate scheduleWithFixedDelay

区别:

ScheduleAtFixedRate 两次任务之间的间隔时间,取决于每次任务执行的时间长短

ScheduleWithFixedDelay 不受任务执行时间长短影响,固定这次任务执行结束后隔x秒执行下一次。

首先看看这两个方法:

public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);

假如每个任务执行花费time时间,如果time>= period,则这次任务执行结束后 立刻执行下一次任务。

如果time<period , 则这次任务执行结束后 ,隔 period-time后执行下一次任务。

 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);

不管任务花费多少时间,当这次任务执行结束,一定要等delay之后,再执行下一次任务。

示例:

scheduleAtFixedRate

public class TestScheduledExecutorService1 {
    public static void main(String[] args) {
        ScheduledExecutorService
            executorService = Executors.newSingleThreadScheduledExecutor();
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName() + "begin == " + System.currentTimeMillis());
                    Thread.currentThread().sleep(3000);  //任务花费大约3秒
                    System.out.println(Thread.currentThread().getName() + "end   == " + System.currentTimeMillis());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        int delay = 1;
        int timespan = 4
        executorService.scheduleAtFixedRate(run, delay, timespan, TimeUnit.SECONDS);
    }

}

执行结果:

pool-1-thread-1begin == 1559986475105
pool-1-thread-1end   == 1559986478106
pool-1-thread-1begin == 1559986479105 //上一次执行结束后,隔1秒(4-3)执行下一次
pool-1-thread-1end   == 1559986482105
pool-1-thread-1begin == 1559986483105
pool-1-thread-1end   == 1559986486106

.......

如果上面的Thread.currentThread().sleep(3000);改为 5000:

System.out.println(Thread.currentThread().getName() + "begin == " + System.currentTimeMillis());
                    Thread.currentThread().sleep(5000);  //任务花费大约5秒
                    System.out.println(Thread.currentThread().getName() + "end   == " + System.currentTimeMillis());

执行结果如下:

pool-1-thread-1begin == 1559986900174
pool-1-thread-1end   == 1559986903175
pool-1-thread-1begin == 1559986903175 //上一次执行结束,立刻执行下一次,因为2-3<=0
pool-1-thread-1end   == 1559986906176
pool-1-thread-1begin == 1559986906176
pool-1-thread-1end   == 1559986909176
pool-1-thread-1begin == 1559986909176
......

 

scheduleWithFixedDelay

public class TestScheduledExecutorService1 {
    public static void main(String[] args) {
        ScheduledExecutorService
            executorService = Executors.newSingleThreadScheduledExecutor();
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName() + "begin == " + System.currentTimeMillis());
                    Thread.currentThread().sleep(3000);  //每个任务花费约3秒
                    System.out.println(Thread.currentThread().getName() + "end   == " + System.currentTimeMillis());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        int delay = 1;
        int timespan = 4;
        executorService.scheduleWithFixedDelay(run, delay, timespan, TimeUnit.SECONDS);
    }
}

执行结果:

pool-1-thread-1begin == 1559986731262
pool-1-thread-1end   == 1559986734262
pool-1-thread-1begin == 1559986738263 //上一次执行结束后,隔4秒执行下一次任务。
pool-1-thread-1end   == 1559986741263
pool-1-thread-1begin == 1559986745264
pool-1-thread-1end   == 1559986748265
.......

如果上面的Thread.currentThread().sleep(3000);改为 5000,执行结果:

pool-1-thread-1begin == 1560072366850
pool-1-thread-1end   == 1560072371852
pool-1-thread-1begin == 1560072375852 //依旧是隔4秒执行下一次任务
pool-1-thread-1end   == 1560072380852
pool-1-thread-1begin == 1560072384853
pool-1-thread-1end   == 1560072389854
......

另外,ScheduledExecutorService 还有其他的方法,比如:

public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                           long delay, TimeUnit unit);

等待delay时间后执行一个实现了Callable的任务,返回执行结果。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值