java ThreadPoolExecutor 线程池构造方法参数:corePoolSize(核心线程数量)、maximumPoolSize(最大线程数量)

ThreadPoolExecutor

构造方法:

public ThreadPoolExecutor(
    int corePoolSize,
    int maximumPoolSize,
    long keepAliveTime,
    TimeUnit unit,
    BlockingQueue<Runnable> workQueue,
    ThreadFactory threadFactory,
    RejectedExecutionHandler handler
)

图解 corePoolSize、maximumPoolSize 参数

执行流程

  1. 当线程数小于核心线程数时,创建线程,并执行。
  2. 当线程数大于等于核心线程数,且任务队列未满时,将任务放入任务队列。
  3. 当线程数大于等于核心线程数,且任务队列已满,分以下两种情况:
    • 若线程数小于最大线程数,创建线程
    • 若线程数等于最大线程数,抛出异常,拒绝任务

corePoolSize 相关属性

设置 allowCoreThreadTimeout=true(默认false)时,核心线程会超时关闭

corePoolSize 相关方法

创建线程池后,调用 prestartCoreThread() 或者 prestartAllCoreThread() 方法,创建 corePoolSize 个 核心线程

corePoolSize 推荐参数

Runtime.getRuntime().availableProcessors() * 1 , 根据压力测试获得最好的核心线程数量

maximumPoolSize 推荐参数

Runtime.getRuntime().availableProcessors() * 4 +1 , 根据压力测试获得最好的最大线程数量

例子

package ThreadPool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Instance1_1 {
    public static void main(String[] args) {
        // 通过源码可知 使用 Executors.defaultThreadFactory() 默认构造方法
        // 使用 new ThreadPoolExecutor.AbortPolicy() 拒绝策略
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1,
                1,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue(1)
        );

        threadPoolExecutor.execute(new MyRun("1"));

        threadPoolExecutor.shutdown();
    }
    static class MyRun implements Runnable {
        String name;

        public MyRun(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            System.out.println(name+"--开始");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(name+"--结束");
        }
    }
}

拒绝策略

提示:这里只是简单介绍拒绝策略,详见 链接

执行流程

  • 添加第1个任务,由于 corePoolSize 是 1,所以会 创建 1 个线程运行

  • 添加第2个任务(第一个任务没有执行结束),此时:线程数(2个)大于核心线程数(1个),但队列大小是 1,且未满,所以将该任务放到队列中(注意:只能放一个任务)

  • 添加第3个任务,此时:线程数(2个)大于核心线程数(1个),且队列也满了,由于 maximumPoolSize 是 1,默认使用 AbortPolicy 拒绝策略,此时会抛异常

package ThreadPool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class 拒绝策略 {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1,
                1,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue(1)
                );

        System.out.println("提交线程1");
        threadPoolExecutor.execute(new MyRun("1",threadPoolExecutor));

        System.out.println("提交线程2");
        threadPoolExecutor.execute(new MyRun("2",threadPoolExecutor));

        System.out.println("提交线程3");
        try {
            /*
             由于 corePoolSize、maximumPoolSize是1,队列大小是1
             也就是只能有1个线程运行,队列只能放1个线程,
             默认使用 AbortPolicy 拒绝策略
             所以添加第3个线程时,会抛异常
             */
            threadPoolExecutor.execute(new MyRun("3", threadPoolExecutor));
        }catch (Exception e){
            System.out.println("拒绝策略,抛出异常:"+e );
        }
        // 关闭线程池
        threadPoolExecutor.shutdown();
    }
    static class MyRun implements Runnable {
        String name;
        ThreadPoolExecutor threadPoolExecutor;

        public MyRun(String name, ThreadPoolExecutor threadPoolExecutor) {
            this.name = name;
            this.threadPoolExecutor = threadPoolExecutor;
        }

        @Override
        public void run() {
            System.out.println("----"+name+" 线程开始");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("===="+name+" 线程结束");
        }
    }
}

执行结果如下:

参数:
https://www.jianshu.com/p/25a266fb1357
https://www.cnblogs.com/waytobestcoder/p/5323130.html
https://segmentfault.com/a/1190000040502271
https://cloud.tencent.com/developer/article/1638175


原文出处:/show_1IX4cGRnLUsW.html