ThreadPoolExecutor构造函数重要参数分析,以及创建线程池

参数分析

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;
    }
  1. 参数分析
  • corePoolSize : 核心线程数定义了最小可以同时运行的线程数量。
  • maximumPoolSize : 当队列中存放的任务达到队列容量的时候,当前可以同时运行的线程数量变为最大线程数。
  • workQueue: 当新任务来的时候会先判断当前运行的线程数量是否达到核心线程数,如果达到的话,新任务就会被存放在队列中。

ThreadPoolExecutor其他常见参数:

  • keepAliveTime:当线程池中的线程数量大于
  • corePoolSize 的时候,如果这时没有新的任务提交,核心线程外的线程不会立即销毁,而是会等待,直到等待的时间超过了 keepAliveTime才会被回收销毁;
  • unit : keepAliveTime 参数的时间单位。
  • threadFactory :executor 创建新线程的时候会用到
  • handler :饱和策略
    • ThreadPoolExecutor.AbortPolicy: 抛出 RejectedExecutionException来拒绝新任务的处理。
    • ThreadPoolExecutor.CallerRunsPolicy: 调用执行自己的线程运行任务,也就是直接在调用execute方法的线程中运行(run)被拒绝的任务,如果执行程序已关闭,则会丢弃该任务。因此这种策略会降低对于新任务提交速度,影响程序的整体性能。如果您的应用程序可以承受此延迟并且你要求任何一个任务请求都要被执行的话,你可以选择这个策略。
    • ThreadPoolExecutor.DiscardPolicy: 不处理新任务,直接丢弃掉。
    • ThreadPoolExecutor.DiscardOldestPolicy: 此策略将丢弃最早的未处理的任务请求。

创建简单线程池Demo

MyRunnable.java

package ThreadPoolExecutorDemo;

import java.util.Date;

//创建一个简单的Runnable类,需要大约5s中来执行其任务
public class MyRunnable implements Runnable{
    private String command;
    public MyRunnable(String s){
        this.command = s;
    }

    @Override
    public void run(){
        System.out.println(Thread.currentThread().getName() + " start. Time = " + new Date());
        processCommand();
        System.out.println(Thread.currentThread().getName() + " End. Time = " + new Date());
    }
    
    private void processCommand(){
        try{
            Thread.sleep(5000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }

    @Override 
    public String toString(){
        return this.command;
    }
}

测试ThreadPoolExecutorDemo.java

corePoolSize: 核心线程数为 5。
maximumPoolSize :最大线程数 10
keepAliveTime : 等待时间为 1L。
unit: 等待时间的单位为 TimeUnit.SECONDS。
workQueue:任务队列为 ArrayBlockingQueue,并且容量为 100;
handler:饱和策略为 CallerRunsPolicy。

package ThreadPoolExecutorDemo;

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

public class ThreadPoolExecutorDemo {
    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final int QUEUE_CAPACITY = 100;
    private static final Long KEEP_ALIVE_TIME = 1L;
    public static void main(String[] args){
        //
        //
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
            CORE_POOL_SIZE,
            MAX_POOL_SIZE,
            KEEP_ALIVE_TIME,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(QUEUE_CAPACITY),
            new ThreadPoolExecutor.CallerRunsPolicy()
        );

        for(int i = 0; i < 10; i++){
            Runnable worker = new MyRunnable("" + i);
            executor.execute(worker);
        }

        //
        executor.shutdown();
        while(!executor.isTerminated()){
        }
        System.out.println("Finished all threads");
    }
}

输出

pool-1-thread-1 start. Time = Thu Aug 18 10:20:30 CST 2022
pool-1-thread-3 start. Time = Thu Aug 18 10:20:30 CST 2022
pool-1-thread-4 start. Time = Thu Aug 18 10:20:30 CST 2022
pool-1-thread-2 start. Time = Thu Aug 18 10:20:30 CST 2022
pool-1-thread-5 start. Time = Thu Aug 18 10:20:30 CST 2022
pool-1-thread-1 End. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-3 End. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-4 End. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-5 End. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-2 End. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-5 start. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-4 start. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-3 start. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-1 start. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-2 start. Time = Thu Aug 18 10:20:35 CST 2022
pool-1-thread-3 End. Time = Thu Aug 18 10:20:40 CST 2022
pool-1-thread-1 End. Time = Thu Aug 18 10:20:40 CST 2022
pool-1-thread-5 End. Time = Thu Aug 18 10:20:40 CST 2022
pool-1-thread-4 End. Time = Thu Aug 18 10:20:40 CST 2022
pool-1-thread-2 End. Time = Thu Aug 18 10:20:40 CST 2022
Finished all threads

流程图+解释

在这里插入图片描述
在代码中模拟了10个任务,配置的核心线程数为5,等待队列容量为100,所以每次只可能存在5个任务同时执行,剩下的5个任务会被放到等待队列中。当前的5个任务中如果有任务被执行完了,现成就会去拿新的任务去执行。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

star__king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值