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
    .......
    

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

  • 相关阅读:
    mysql视图定义以及创建视图
    前端页面调用spring mvc接口发生跨域问题解决方案
    亲测可用的ssm框架搭建
    python--pandas基本操作
    jieba中文处理 python
    爬虫 登录开心网,登录人人网
    Selenium&wevdriver
    django 中间件的一些基本操作
    写一个简单的登录客户端步骤
    个人对ADO连接池的理解
  • 原文地址:https://www.cnblogs.com/Kevin-1992/p/12608373.html
Copyright © 2011-2022 走看看