zoukankan      html  css  js  c++  java
  • 线程池(5)Executors.newScheduledThreadPool

    例子1(scheduleAtFixedRate):延迟2秒后,每隔3秒执行1次

    ScheduledExecutorService es = Executors.newScheduledThreadPool(5);
            log.info("开始时间");
            Runnable syncRunnable = new Runnable() {
                @Override
                public void run() {
                    log.info(Thread.currentThread().getName());
                }
            };
            es.scheduleAtFixedRate(syncRunnable, 2000, 3000, TimeUnit.MILLISECONDS);

    运行结果:

                10:49:22.495 开始时间
                10:49:24.500 pool-1-thread-1
                10:49:27.499 pool-1-thread-1
                10:49:30.500 pool-1-thread-2
                10:49:33.500 pool-1-thread-1
                10:49:36.501 pool-1-thread-3
                10:49:39.500 pool-1-thread-2
                10:49:42.500 pool-1-thread-2
                10:49:45.500 pool-1-thread-1
                10:49:48.501 pool-1-thread-1
                10:49:51.501 pool-1-thread-1
                10:49:54.501 pool-1-thread-4
                10:49:57.501 pool-1-thread-2
                10:50:00.502 pool-1-thread-2
                10:50:03.502 pool-1-thread-3
                10:50:06.502 pool-1-thread-3
                10:50:09.502 pool-1-thread-3
                10:50:12.503 pool-1-thread-5
                10:50:15.503 pool-1-thread-5
                10:50:18.503 pool-1-thread-5
                10:50:21.503 pool-1-thread-5
                10:50:24.503 pool-1-thread-3
                10:50:27.503 pool-1-thread-2
                10:50:30.503 pool-1-thread-1
                10:50:33.504 pool-1-thread-4
                10:50:36.504 pool-1-thread-5
                10:50:39.504 pool-1-thread-5
                10:50:42.504 pool-1-thread-2

    例子2(scheduleWithFixedDelay):延迟5秒后,每个任务执行完后延迟3秒在执行1次

    ScheduledExecutorService es = Executors.newScheduledThreadPool(5);
            log.info("开始时间");
            Runnable syncRunnable = new Runnable() {
                @Override
                public void run() {
                    log.info(Thread.currentThread().getName());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            es.scheduleWithFixedDelay(syncRunnable, 5000, 3000, TimeUnit.MILLISECONDS);

    运行结果:

                11:10:27.773 开始时间
                11:10:32.778 pool-1-thread-1
                11:10:36.779 pool-1-thread-1
                11:10:40.780 pool-1-thread-2
                11:10:44.781 pool-1-thread-1
                11:10:48.783 pool-1-thread-3
                11:10:52.785 pool-1-thread-3
                11:10:56.785 pool-1-thread-4
                11:11:00.787 pool-1-thread-4
                11:11:04.788 pool-1-thread-4
                11:11:08.789 pool-1-thread-4
                11:11:12.790 pool-1-thread-3
                11:11:16.792 pool-1-thread-1

    本来是每隔3秒执行的,但是,由于某个任务处理时间过长,导致延后。本例是延后1秒,即4秒。

    总结:scheduleAtFixedRate与scheduleWithFixedDelay区别

    scheduleAtFixedRate:不管任务是否执行完了,在3秒内必须执行
    scheduleWithFixedDelay:等任务执行完了,在等3秒后执行
    因此,scheduleWithFixedDelay 非常有用。

  • 相关阅读:
    Zookeeper(1)---初识
    golang的一些零散笔记
    ELK使用过程中遇到的一些问题
    ECharts系列:玩转ECharts之常用图(折线、柱状、饼状、散点、关系、树)
    MySQL系列:Docker安装 MySQL提示错误:Access denied for user'root'@'localhost' (using password:yes)
    HTML+CSS系列:登录界面实现
    Apollo系列(二):ASP.NET Core 3.1使用分布式配置中心Apollo
    Apollo系列(一):分布式配置中心Apollo安装(Linux、Docker)
    为你的应用加上skywalking(链路监控)
    工作中,你是如何开始搭建一套容器云环境的呢?
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/9606559.html
Copyright © 2011-2022 走看看