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

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

  • 相关阅读:
    c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明
    无废话MVC入门教程二[第一个小Demo]
    winform使用xml作为数据源
    SQL Server 2005为数据库设置用户名和密码的方法
    数据库 的版本为 661,无法打开。此服务器支持 655 版及更低版本。不支持降级路径。
    Visual Studio快速封装字段方法
    erp crm oa
    sqldbhelper
    OleDbHelper
    存储过程分页(3)
  • 原文地址:https://www.cnblogs.com/Kevin-1992/p/12608373.html
Copyright © 2011-2022 走看看