线程池Executors.newScheduledThreadPool

Posted yaoyuan2

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程池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 非常有用。

 



以上是关于线程池Executors.newScheduledThreadPool的主要内容,如果未能解决你的问题,请参考以下文章

什么叫线程池?线程池如何使用?

多线程(六):线程池

多线程(六):线程池

十五、线程池(六)自动创建线程池的弊端

java 如何获得线程池中正在执行的线程数?

线程池参数与线程池调优