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

scheduleAtFixedRate和scheduleWithFixedDelay探究

时间:2020-02-05 16:37:15      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:顺序   java   shm   pool   lam   二次   seconds   ack   间隔   

scheduleAtFixedRate是用任务开始时间计算间隔,就是说某任务上次启动时间+间隔时间就是下次启动时间。
scheduleWithFixedDelay是用任务结束时间计算间隔,就是说某任务上次结束时间+间隔时间就是下次启动时间。

这段代码模拟了一组10个任务,每个任务都有个name(任务名)和time(任务花费的时间)。第6个任务(任务名为任务5)耗时15s,其他任务耗时1s。这一组任务分别用scheduleAtFixedRate(固定频率)和scheduleWithFixedDelay(固定延迟)处理,截取部分日志解析这两个方法的运行逻辑。


import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@Slf4j
public class MyConcurrentTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService readThreadPool = Executors.newScheduledThreadPool(5);
        List<ScheduledFuture> futureList = new ArrayList<>();
        //用objects模拟任务列表,任务的time代表耗费的时间
        List<Map<String,Object>> objects = new ArrayList<>();
        for(int i = 0 ; i < 10 ; i++){
            Map<String,Object> object = new HashMap<>();
            object.put("name","任务"+i);
            object.put("time",1000);
            objects.add(object);
        }
        //第6个任务sleep15s,第6个任务是任务5
        objects.get(5).put("time",15000);
        System.out.println(objects);
        for(Map<String,Object> object : objects){
            //任务在0s后开始,任务间隔是5s
            ScheduledFuture future = readThreadPool.scheduleAtFixedRate(()->{
                log.info(object.toString());
                try {
                    //暂停指定时间
                    Thread.sleep((int)object.get("time"));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },0,5, TimeUnit.SECONDS);
            futureList.add(future);
        }
        for(ScheduledFuture future:futureList){
            future.get();
        }
    }
}

用scheduleAtFixedRate(固定频率)处理的日志。

--10个任务的详情
[{name=任务0, time=1000}, {name=任务1, time=1000}, {name=任务2, time=1000}, {name=任务3, time=1000}, {name=任务4, time=1000}, {name=任务5, time=15000}, {name=任务6, time=1000}, {name=任务7, time=1000}, {name=任务8, time=1000}, {name=任务9, time=1000}]
--线程池有5个核心线程,同时开始了5个任务。
11:06:06.505 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务4, time=1000}
11:06:06.505 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务0, time=1000}
11:06:06.505 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务1, time=1000}
11:06:06.505 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务3, time=1000}
11:06:06.505 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务2, time=1000}
--1s之后任务处理完,处理其他的任务。
11:06:07.505 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务5, time=15000}
11:06:07.505 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务9, time=1000}
11:06:07.505 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务6, time=1000}
11:06:07.505 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务7, time=1000}
11:06:07.505 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务8, time=1000}
--4s之后距离任务0/1/2/3/4上次启动已经过去了5s,任务0/1/2/3/4都已经准备好第二次执行,但此时只有四个空闲线程。按照顺序任务4没有执行。
11:06:11.505 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务0, time=1000}
11:06:11.505 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务1, time=1000}
11:06:11.505 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务2, time=1000}
11:06:11.505 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务3, time=1000}
--1s之后除了任务4,任务5/6/7/8/9也都准备好第二次执行,但此时任务5第一次执行还没有结束,所以任务5第二次执行被阻塞。按照顺序,任务9没有执行。
11:06:12.506 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务6, time=1000}
11:06:12.506 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务8, time=1000}
11:06:12.506 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务4, time=1000}
11:06:12.506 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务7, time=1000}
--1s之后任务5/9准备好执行,此时任务5第二次执行依然被阻塞。
11:06:13.521 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务9, time=1000}
--3s之后任务0/1/2/3准备好第三次执行……
11:06:16.506 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务0, time=1000}
11:06:16.506 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务2, time=1000}
11:06:16.506 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务1, time=1000}
11:06:16.506 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务3, time=1000}
--1s之后……
11:06:17.506 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务4, time=1000}
11:06:17.506 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务7, time=1000}
11:06:17.506 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务8, time=1000}
11:06:17.506 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务6, time=1000}
--1s之后……
11:06:18.506 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务9, time=1000}
--3s之后……
11:06:21.507 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务1, time=1000}
11:06:21.507 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务3, time=1000}
11:06:21.507 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务0, time=1000}
11:06:21.507 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务2, time=1000}
--1s之后,任务5第二次执行,因为任务5第一次执行开始时间到现在时间间隔大于规定的时间间隔,即任务5第二次执行已经“迟到”,应立即执行。
11:06:22.507 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务5, time=15000}
11:06:22.522 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务7, time=1000}
11:06:22.522 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务8, time=1000}
11:06:22.522 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务4, time=1000}
11:06:22.522 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务6, time=1000}
11:06:23.523 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务9, time=1000}

综上所述,scheduleAtFixedRate(固定频率)是在任务上次开始时间+间隔时间开始的,拿任务0来说,第一次启动是在11:06:06,第二次启动是在11:06:11,中间间隔了5s,这正是我们规定的间隔时间,以此来达到频率固定的效果(当然频率也不是绝对固定的,如任务4)。如果任务执行时间过长,任务依然会添加到任务队列,不过会阻塞,等任务执行完之后才会再次执行。

注意:如果任务都是第一次处理很耗时间,之后耗时很短的话,会出现连续处理多次某个任务的情况。上面的代码,我们在lambda表达式最后一行添加object.put("time",0);,在处理往第一遍之后把所有的任务的处理时间全都设成0s,这样当任务5执行完之后,任务队列里就会有3个耗时为0s的任务5,而此时,这三个任务都已经超时,且不再是阻塞状态,会同时执行任务5三遍。如下日志:

12:48:14.490 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务4, time=1000}
12:48:14.490 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务0, time=1000}
12:48:14.490 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务2, time=1000}
12:48:14.490 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务1, time=1000}
12:48:14.490 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务3, time=1000}
12:48:15.490 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务7, time=1000}
12:48:15.490 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务9, time=1000}
12:48:15.490 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务6, time=1000}
12:48:15.490 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务8, time=1000}
12:48:15.490 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务5, time=15000}
12:48:19.507 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务0, time=0}
12:48:19.507 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务1, time=0}
12:48:19.507 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务2, time=0}
12:48:19.507 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务3, time=0}
12:48:19.507 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务4, time=0}
12:48:19.507 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务6, time=0}
12:48:19.507 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务7, time=0}
12:48:19.507 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务8, time=0}
12:48:19.507 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务9, time=0}
12:48:24.507 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务1, time=0}
12:48:24.507 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务0, time=0}
12:48:24.507 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务2, time=0}
12:48:24.507 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务3, time=0}
12:48:24.507 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务4, time=0}
12:48:24.507 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务6, time=0}
12:48:24.507 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务7, time=0}
12:48:24.507 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务8, time=0}
12:48:24.507 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务9, time=0}
12:48:29.509 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务1, time=0}
12:48:29.509 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务2, time=0}
12:48:29.509 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务3, time=0}
12:48:29.509 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务0, time=0}
12:48:29.509 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务4, time=0}
12:48:29.509 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务6, time=0}
12:48:29.509 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务7, time=0}
12:48:29.509 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务8, time=0}
12:48:29.509 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务9, time=0}
--连续执行了三遍任务5
12:48:30.493 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务5, time=0}
12:48:30.493 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务5, time=0}
12:48:30.493 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务5, time=0}

用scheduleWithFixedDelay(固定延迟)处理的日志。

--10个任务的详情
[{name=任务0, time=1000}, {name=任务1, time=1000}, {name=任务2, time=1000}, {name=任务3, time=1000}, {name=任务4, time=1000}, {name=任务5, time=15000}, {name=任务6, time=1000}, {name=任务7, time=1000}, {name=任务8, time=1000}, {name=任务9, time=1000}]
--线程池有5个核心线程,同时开始了5个任务。
11:51:51.450 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务4, time=1000}
11:51:51.450 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务1, time=1000}
11:51:51.450 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务0, time=1000}
11:51:51.450 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务2, time=1000}
11:51:51.450 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务3, time=1000}
--1s之后任务处理完,处理其他的任务。
11:51:52.466 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务7, time=1000}
11:51:52.466 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务9, time=1000}
11:51:52.466 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务6, time=1000}
11:51:52.466 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务8, time=1000}
11:51:52.466 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务5, time=15000}
--5s之后,距离任务0/1/2/3/4上次处理结束已经过去了5s,任务0/1/2/3/4都已经准备好第二次执行,但此时只有四个空闲线程。按照顺序任务4没有执行。
11:51:57.467 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务0, time=1000}
11:51:57.467 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务2, time=1000}
11:51:57.467 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务1, time=1000}
11:51:57.467 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务3, time=1000}
--1s之后除了任务4,任务6/7/8/9也都准备好第二次执行,任务5还没有结束第一次执行。
11:51:58.467 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务9, time=1000}
11:51:58.467 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务4, time=1000}
11:51:58.467 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务6, time=1000}
11:51:58.467 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务7, time=1000}
--1s之后执行任务8。
11:51:59.483 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务8, time=1000}
--4s之后任务0/1/2/3准备好第三次执行……
11:52:03.468 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务3, time=1000}
11:52:03.468 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务1, time=1000}
11:52:03.468 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务2, time=1000}
11:52:03.468 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务0, time=1000}
--1s之后……
11:52:04.484 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务7, time=1000}
11:52:04.484 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务6, time=1000}
11:52:04.484 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务4, time=1000}
11:52:04.484 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务9, time=1000}
--1s之后……
11:52:05.500 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务8, time=1000}
--4s之后……
11:52:09.469 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务0, time=1000}
11:52:09.469 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务1, time=1000}
11:52:09.469 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务3, time=1000}
11:52:09.469 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务2, time=1000}
--1s之后……
11:52:10.501 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务4, time=1000}
11:52:10.501 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务7, time=1000}
11:52:10.501 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务6, time=1000}
11:52:10.501 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务9, time=1000}
--1s之后……
11:52:11.501 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务8, time=1000}
--1s之后距离任务5处理(11:51:52--11:52:07)结束已经过去了5s,任务5第二次执行。
11:52:12.471 [pool-1-thread-1] INFO MyConcurrentTest - {name=任务5, time=15000}
11:52:15.471 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务1, time=1000}
11:52:15.471 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务0, time=1000}
11:52:15.471 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务3, time=1000}
11:52:15.471 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务2, time=1000}
11:52:16.518 [pool-1-thread-4] INFO MyConcurrentTest - {name=任务4, time=1000}
11:52:16.518 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务7, time=1000}
11:52:16.518 [pool-1-thread-2] INFO MyConcurrentTest - {name=任务9, time=1000}
11:52:16.518 [pool-1-thread-3] INFO MyConcurrentTest - {name=任务6, time=1000}
11:52:17.534 [pool-1-thread-5] INFO MyConcurrentTest - {name=任务8, time=1000}

综上所述,scheduleWithFixedDelay(固定延迟)是在任务上次结束时间+间隔时间开始的,任务如果没有结束,任务队列是没有下次执行计划的。

scheduleAtFixedRate和scheduleWithFixedDelay探究

标签:顺序   java   shm   pool   lam   二次   seconds   ack   间隔   

原文地址:https://www.cnblogs.com/macho8080/p/12264128.html

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