阿里巴巴规范:手动创建线程池,效果会更好哦 的解决办法,手动创建线程池ThreadPoolExecutor

手动创建线程池

在使用Executors创建线程时,阿里巴巴规范提出了手动创建线程池,效果会更好哦。 使用ThreadPoolExecutor方式创建线程池,可以规避资源耗尽风险(OOM)

ThreadPoolExecutor的构造函数

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

Alibaba规范警告信息

线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。 说明:Executors返回的线程池对象的弊端如下:
1)FixedThreadPool和SingleThreadPool:
  允许的请求队列长度为Integer.MAX_VALUE,可能会堆积大量的请求,从而导致OOM。
2)CachedThreadPool:
  允许的创建线程数量为Integer.MAX_VALUE,可能会创建大量的线程,从而导致OOM。
            
Positive example 1:
    //org.apache.commons.lang3.concurrent.BasicThreadFactory
    ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
        new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
       
        
            
Positive example 2:
    ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
        .setNameFormat("demo-pool-%d").build();

    //Common Thread Pool
    ExecutorService pool = new ThreadPoolExecutor(5, 200,
        0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    pool.execute(()-> System.out.println(Thread.currentThread().getName()));
    pool.shutdown();//gracefully shutdown
       
        
            
Positive example 3:
    <bean id="userThreadPool"
        class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="10" />
        <property name="maxPoolSize" value="100" />
        <property name="queueCapacity" value="2000" />

    <property name="threadFactory" value= threadFactory />
        <property name="rejectedExecutionHandler">
            <ref local="rejectedExecutionHandler" />
        </property>
    </bean>
    //in code
    userThreadPool.execute(thread);

线程执行的逻辑图与说明

逻辑图

202112041259093 (1)

逻辑说明

  • 判断核心线程数是否已满,核心线程数大小和corePoolSize参数有关
  • 若核心线程池已满,判断队列是否满,队列是否满和workQueue参数有关
  • 若队列已满,判断线程池是否已满,线程池是否已满和maximumPoolSize参数有关
  • 若线程池已满,则采用拒绝策略处理无法执执行的任务,拒绝策略和handler参数有关

ThreadPoolExecutor的构造方法参数说明:

  • corePoolSize => 线程池核心线程数量,他来决定新的任务是创建新线程执行,还是丢到workQueue任务队列中去
  • maximumPoolSize => 线程池最大数量,这个参数会根据你使用的workQueue任务队列的类型,决定线程池创建的最大数量
  • keepAliveTime => 空闲线程存活时间,当空闲线程数量超过corePoolSize时,多余线程会在到存活时间时被销毁
  • unit => 时间单位,keepAliveTime的时间单位
  • workQueue => 线程池所使用的任务队列,它被添加到线程池中,但尚未被执行的任务;它一般分为直接提交队列、有界任务队列、无界任务队列、优先任务队列几种
  • threadFactory => 线程池创建线程使用的工厂,工厂名字等,一般用于默认即可
  • handler => 线程池对拒绝任务的处理策略,当任务太多,如何拒绝任务

我们可以使用Alibaba规范来创建线程

完整代码

package com.demos.date.thread;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import java.util.concurrent.*;

public class Thread01 {
    public static void main(String[] args) {
        // 编写线程的名字,一般使用默认即可,
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNamePrefix("demo-pool-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(2, 200,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1024),namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        // 调用线程
        for (int i = 0; i < 4; i++) {
            pool.execute(()->{
                System.out.println(Thread.currentThread().getName()+Thread.currentThread().getId());
                pool.shutdown();
            });
        }
    }
}
posted @ 2021-12-04 13:04  HeiDaotu  阅读(1480)  评论(0编辑  收藏  举报