zoukankan      html  css  js  c++  java
  • 定时线程池执行任务时任务执行时间与定时关系

    当执行时间小于定时时间的时候:

    import lombok.extern.slf4j.Slf4j;
    
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    @Slf4j
    public class ScheduledThreadPoolExample {
    
        public static void main(String[] args) {
            System.out.println("执行的时间小于设定的周期");
            ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
    
            service.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    try {
                        log.info("start task");
                        Thread.sleep(5000);
                        log.info("done");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, 3000, 8000, TimeUnit.MILLISECONDS);
        }
    }
    
    

    执行结果:

    执行的时间小于设定的周期
    15:39:11.944 [pool-1-thread-1] INFO com.hovel.base.thread.pool.SchExample - start task
    15:39:16.960 [pool-1-thread-1] INFO com.hovel.base.thread.pool.SchExample - done
    15:39:19.938 [pool-1-thread-1] INFO com.hovel.base.thread.pool.SchExample - start task
    15:39:24.951 [pool-1-thread-1] INFO com.hovel.base.thread.pool.SchExample - done
    15:39:27.936 [pool-1-thread-1] INFO com.hovel.base.thread.pool.SchExample - start task
    .......
    
    

    当执行任务时间大于定时时间

    import lombok.extern.slf4j.Slf4j;
    
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    @Slf4j
    public class ScheduledThreadPoolExample {
    
        public static void main(String[] args) {
            System.out.println("执行的时间小于设定的周期");
            ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
    
            service.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    try {
                        log.info("start task");
                        Thread.sleep(8000);
                        log.info("done");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, 3000, 5000, TimeUnit.MILLISECONDS);
        }
    }
    

    结果:

    执行的时间大于设定的周期
    15:44:31.404 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - start task
    15:44:39.414 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - done
    15:44:39.414 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - start task
    15:44:47.414 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - done
    15:44:47.414 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - start task
    15:44:55.415 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - done
    15:44:55.415 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - start task
    .......
    

    由上面的两组执行结果对比,可知,定时任务都是串行执行的。当任务执行的时间小于定时时间的时候,定时的时间减去任务执行的时间即是下一次任务执行的时间;但是当任务执行时间大于定时时间的时候就有所不同了,即是定时时长小于任务时长,但是依然会等到任务结束后再执行下一次定时任务,而不是在任务未执行完时就开始下一次任务,只不过这个时候,下一次执行紧挨执行。

  • 相关阅读:
    JVM的学习5_____垃圾回收:分代收集算法
    JVM的学习4____GC的作用和垃圾的标记
    JVM的学习3_____逃逸分析与栈上分配
    JVM的学习2____对象实例的内存分配原理
    JVM的学习1_____内存模型
    SpringMVC的学习____6.JSON 和Ajax
    两种方法关联控制器和DOM
    img的src,a的href使用{{}}设置属性不能生效
    ng之{{value}}顺序
    ng之ng-app指令
  • 原文地址:https://www.cnblogs.com/Kevin-1992/p/12608373.html
Copyright © 2011-2022 走看看