当前位置:   article > 正文

线程池的三种使用方式_executors.newsinglethreadexecutor()

executors.newsinglethreadexecutor()

线程池的三种使用方式

**注意:**下面这三种是 用 Executors 很常见的创建方式,但是阿里巴巴手册不推荐
ThreadPoolExecutor: 是ExecutorService的一个实现类,也是java中最常用的线程池类

  • 【强制】线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写的同学更加明确线程池的运行 规则,规避资源耗尽的风险。 说明: Executors 返回的线程池对象的弊端如下:

    1) FixedThreadPool 和 SingleThreadPool:允许的请求队列长度为 Integer.MAX_VALUE,可能会堆积大量的请求,从而导致 OOM。

    2) CachedThreadPool 和 ScheduledThreadPool:允许的创建线程数量为 Integer.MAX_VALUE, 可能会创建大量的线程,从而导致 OOM。

1、 Executors.newSingleThreadExecutor ()

public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

主要特点如下:

  • 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序执行。

  • newSingleThreadExecutor将corePoolSize和maximumPoolSize都设置为1,它使用的LinkedBlockingQueue。

  • **应用场景:**SingleThreadExecutor是只有一个线程的线程池,常用于需要让线程顺序执行,并且在任意时间,只能有一个任务被执行,而不能有多个线程同时执行的场景。

示例:

/**
     *  @description: 线程池大小只有一个
     *  @author sxp
     *  @date 2021/10/21 14:11
     */
    public static void singleThreadExecutor() {
        /**
         *  @description: 线程池使用步骤:1、创建线程池
         */
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        //线程池要关闭,一般关闭我们放在finally中执行
        try {
            for (int i = 0; i < 10; i++) {
                /**
                 *  @description:线程池使用步骤:2、线程池执行线程
                 */
                final int temp = i;
                executorService.execute(() -> System.out.println(Thread.currentThread().getName() + ":running"+"第"+temp+"个线程"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            /**
             *  @description:线程池使用步骤:3、关闭线程池
             */
            executorService.shutdown();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

2、Executors.newFixedThreadPool(int)

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}
  • 1
  • 2
  • 3
  • 4
  • 5

主要特点如下:

  • 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。

  • newFixedThreadPool创建的线程池corePoolSize和maximumPoolSize值是相等的,它使用的LinkedBlockingQueue。

  • **应用场景:**FixedThreadPool是线程数量固定的线程池,适用于为了满足资源管理的需求,而需要适当限制当前线程数量的情景,适用于负载比较重的服务器。

示例:

/**
     *  @description: 超过线程池大小部分,将拒绝
     *  @author sxp
     *  @date 2021/10/21 14:30
     */
    public static void fixedThreadPool() {
        /**
         *  @description: 线程池使用步骤:1、创建线程池
         */
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        //线程池要关闭,一般关闭我们放在finally中执行
        try {
            for (int i = 0; i < 10; i++) {
                /**
                 *  @description:线程池使用步骤:2、线程池执行线程
                 */
                executorService.execute(() -> System.out.println(Thread.currentThread().getName() + ":running"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            /**
             *  @description:线程池使用步骤:3、关闭线程池
             */
            executorService.shutdown();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

3、Executors.newCachedThreadPool()
底层源码:

public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

主要特点如下:

  • 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程
  • newCachedThreadPool将corePoolSize设置为0,将maximumPoolSize设置为Integer.MAX_VALUE,使用的SynchronousQueue,也就是说来了任务就创建线程运行,当线程空闲超过60秒,就销毁线程。

示例:

/**
     *  @description: 线程池大小根据请求量自动扩张
     *  @author sxp
     *  @date 2021/10/21 14:40
     */
    public static void cachedThreadPool() {
        /**
         *  @description: 线程池使用步骤:1、创建线程池
         *  可以弹性伸缩的线程池
         */
        ExecutorService executorService = Executors.newCachedThreadPool();
        //线程池要关闭,一般关闭我们放在finally中执行
        try {
            for (int i = 0; i < 10; i++) {
                /**
                 *  @description:线程池使用步骤:2、线程池执行线程
                 */
                executorService.execute(() -> System.out.println(Thread.currentThread().getName() + ":running"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            /**
             *  @description:线程池使用步骤:3、关闭线程池
             */
            executorService.shutdown();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/246571
推荐阅读
相关标签
  

闽ICP备14008679号