zoukankan      html  css  js  c++  java
  • ScheduledThreadPoolExecutor周期任务或延时任务线程池

    ScheduledThreadPoolExecutor可以代替timer,timer的缺点是一个timer启动一个线程,如果任务数量很多,会创建很多线程,不推荐使用。

    ScheduledThreadPoolExecutor他有个线程池管理线程管理所有任务,效率更高

    public class ScheduledThreadPoolExecutorTest {
    
        public static void main(String[] args) throws InterruptedException {
            //
            ScheduledThreadPoolExecutor schedulePool = new ScheduledThreadPoolExecutor(4);
    
    //      周期任务
    //      从上一周期的任务的开始时间到当前周期的任务开始时间的间隔
    //      任务的执行时间 > period参数:时间间隔是任务的执行时间。
    //      任务的执行时间 < period参数:时间间隔是period参数。
            ScheduledFuture<?> scheduledFuture = schedulePool.scheduleAtFixedRate(new MyRunnable(), 50, 5000, TimeUnit.MILLISECONDS);
    
    
    //      取消该任务
            Thread.sleep(20000);
            scheduledFuture.cancel(true);
    
    
    //      延时任务
    //      从上一周期的任务的执行结束时间到当前周期的任务开始时间的间隔,固定延迟时间为delay参数,与任务执行时间无关。
    //      schedulePool.scheduleWithFixedDelay(new MyRunnable(), 50, 1000, TimeUnit.MILLISECONDS);
    
    
    
        }
    
        static class MyRunnable implements Runnable {
    
            int period = 1;
    
            @Override
            public void run() {
                //为周期任务捕获异常,避免异常影响下一周期的任务执行
                try {
                    System.out.println("---------------第 " + period + " 周期-------------");
                    System.out.println("begin = " + System.currentTimeMillis() / 1000);////任务执行时间
                    Thread.sleep(2000);
                    System.out.println("end =   " + System.currentTimeMillis() / 1000);
                    period++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
            }
        }
    
    
        /**
         * 计算初始的延迟毫秒数
         * @return
         */
        public    static  long   getDelayMillis(){
            int dayOfHour = 0; // 24h 制
            int minute = 0;
            int second = 0;
            long delay;  //首次执行任务的延迟
            Calendar c = Calendar.getInstance();
            long currentTime = c.getTimeInMillis();
            c.set(Calendar.HOUR_OF_DAY, dayOfHour);
            c.set(Calendar.MINUTE, minute);
            c.set(Calendar.SECOND, second);
            long executeTime = c.getTimeInMillis();
            delay = executeTime - currentTime < 0 ? (executeTime + 24 * 60 * 60 * 1000 - currentTime )
                    : (executeTime - currentTime);
            System.out.println("DelayTimeInMillis =" + delay);
            return  delay;
        }
    }
  • 相关阅读:
    【程序员工具】7z压缩解压缩软件-又轻又快
    【2021】想好要做什么,然后就放手去做吧!
    Maven下载源换成国内镜像
    漏洞扫描工具
    Python中文分词库
    Python pip更换国内安装源
    Q库存销售订单退货不能确立帐户问题(带WBS的销售退货订单)
    获取用户信息BAPI BAPI_USER_GET_DETAIL
    成本价自动创建功能自动函数
    MR21 创建标准价函数摘自标准功能
  • 原文地址:https://www.cnblogs.com/moris5013/p/11917299.html
Copyright © 2011-2022 走看看