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.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }
  • int corePoolSize 核心线程数(随时就绪的状态)
  • int maximumPoolSize 最大线程数(极限情况下,线程池最多有多少线程)
  • long keepAliveTime 空闲线程存活时间(非核心线程在无任务情况下要多久释放)
  • TimeUnit unit 空闲线程存活时间的单位:分钟和秒
  • BlockingQueue<Runnable> workQueue 工作队列(用于存放线程执行任务)
  • ThreadFactory threadFactory 线程工程(掌控每个线程的生产和线程的属性,比如线程名)
  • RejectedExecutionHandler handler 拒绝策略(任务队列满的时候我们采取的措施,比如抛异常,自定义策略)

配置

配置类

package com.xin.springbootinittemplate.config;

import com.xin.springbootinittemplate.handler.ThreadPoolExecutorFactory;
import com.xin.springbootinittemplate.handler.ThreadPoolExecutorHandler;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.*;

/**
 * @Author Retrograde-LX
 * @Date 2024/08/07 下午 2:21
 * @Version 1.0
 * @Remark 又是程序猿秃头的一天
 */
public class ThreadPoolExecutorConfig {
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(1000);
        ThreadFactory threadFactory = new ThreadPoolExecutorFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutorHandler();

        return new ThreadPoolExecutor(
                5,
                10,
                60,
                TimeUnit.SECONDS,
                workQueue,
                threadFactory,
                handler);
    }
}

线程工程:

package com.xin.springbootinittemplate.handler;

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.ThreadFactory;

/**
 * @Author Retrograde-LX
 * @Date 2024/08/07 下午 2:24
 * @Version 1.0
 * @Remark 又是程序猿秃头的一天
 */
public class ThreadPoolExecutorFactory implements ThreadFactory {
    @Override
    public Thread newThread(@NotNull Runnable r) {
        Thread thread = new Thread(r);
        thread.setName("业务的线程名称");
        return thread;
    }
}

拒绝策略:

package com.xin.springbootinittemplate.handler;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author Retrograde-LX
 * @Date 2024/08/07 下午 2:26
 * @Version 1.0
 * @Remark 又是程序猿秃头的一天
 */
public class ThreadPoolExecutorHandler implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        // 拒绝策略(也可以利用消息队列来排队处理,或者保存到数据库中再利用定时任务来处理)
        System.out.println("Task" + r.toString() + "rejected from" + executor.toString());
    }
}

使用

在需要使用的地方注入线程池对象

	@Resource
    private ThreadPoolExecutor threadPoolExecutor;

调用:

	CompletableFuture.runAsync(() -> {
            // 业务代码
    }, threadPoolExecutor);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Retrograde-lx

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

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

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

打赏作者

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

抵扣说明:

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

余额充值