• 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;
        }
    }
  • 相关阅读:
    Linux驱动下的platform总线架构(转)
    一生中很值得看的30个故事之一断箭
    学习嵌入式LINUX系统的笔记和体会
    DIY:自己动手做一个迷你 Linux 系统
    linux里面i386 i686 i486 i586代表什么?是什么意思
    菜鸟编译Linux内核
    LINUX核心重编与升级
    ARM 内核移植中常见的错误
    Linux 2.6.19.x 内核编译配置选项简介
    基于S3C2410平台移植Linux 2.6内核指南
  • 原文地址:https://www.cnblogs.com/moris5013/p/11917299.html
Copyright © 2020-2023  润新知