TPE-ThreadPoolExecutor

 

TPE:

java.util.concurrent.ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          @NotNull TimeUnit unit,
                          @NotNull BlockingQueue<Runnable> workQueue,
                          @NotNull ThreadFactory threadFactory,
                          @NotNull RejectedExecutionHandler handler)
Creates a new ThreadPoolExecutor with the given initial parameters.
Parameters:
	corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
					在池中保持的线程数,即使它们是空闲的,除非设置了allowCoreThreadTimeOut
	maximumPoolSize - the maximum number of threads to allow in the pool
					在池中允许的最大线程数
	keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
					当线程数大于核心数时,这是多余空闲线程在终止前等待新任务的最大时间。
	unit - the time unit for the keepAliveTime argument
					时间单位的keepAliveTime参数
	workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
					队列用于在执行任务之前执行任务。此队列只保留执行方法提交的可运行任务。
	threadFactory - the factory to use when the executor creates a new thread
					在执行器创建新线程时使用的工厂
	handler - the handler to use when execution is blocked because the thread bounds and queue capacities are reached
					在执行被阻塞时使用的handler,因为到达了线程边界和队列容量
Throws:
	IllegalArgumentException - if one of the following holds: 
		corePoolSize < 0 
		keepAliveTime < 0 
		maximumPoolSize <= 0 
		maximumPoolSize < corePoolSize
	NullPointerException - if workQueue or threadFactory or handler is null

allowCoreThreadTimeOut -- If false (default), core threads stay alive even when idle. If true, core threads use keepAliveTime to time out waiting for work
如果false(默认),核心线程即使在空闲时仍然存活。如果是true,核心线程使用keepAliveTime来等待工作
也就是说,如果allowCoreThreadTimeOut不改变(即:false), 则corePoolSize计数中的Thread即便是idle空闲的,也保持alive状态;
如果被设置成true了,此时keepAliveTime就派上用场了,

 

JDK:

 

ThreadPoolExecutor

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory,
                          RejectedExecutionHandler handler)
用给定的初始参数创建新的 ThreadPoolExecutor。
参数:
corePoolSize - 池中所保存的线程数,包括空闲线程。
maximumPoolSize - 池中允许的最大线程数。
keepAliveTime - 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。
unit - keepAliveTime 参数的时间单位。
workQueue - 执行前用于保持任务的队列。此队列仅保持由 execute 方法提交的 Runnable 任务。
threadFactory - 执行程序创建新线程时使用的工厂。
handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序。
抛出:
IllegalArgumentException - 如果 corePoolSize 或 keepAliveTime 小于 0,或者 maximumPoolSize 小于等于 0,或者 corePoolSize 大于 maximumPoolSize。
NullPointerException - 如果 workQueue、 threadFactory 或 handler 为 null。

 

 

 

 

 

 

 

 

/* * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved. */ package com.huawei.it.gaia.apigc.service.common; import com.google.common.util.concurrent.ThreadFactoryBuilder; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; /** * 功能描述 * * @since 2023-01-19 */ @Configuration public class ExecPoolAutoConfig { /** * 定时任务执行线程池配置 * * @return 线程池 */ @Value("${schedule.poolSize}") private int poolSize; @Bean public Executor scheduleTaskExecPool() { ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor(); pool.setCorePoolSize(poolSize); pool.setQueueCapacity(100); pool.setMaxPoolSize(5); pool.setThreadFactory( new ThreadFactoryBuilder().setDaemon(false).setNameFormat("schedule-task-pool-%d").build()); pool.initialize(); pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); return pool; } @Bean public Executor kafkaTopic1TaskExecPool() { ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor(); pool.setCorePoolSize(1); pool.setMaxPoolSize(1); pool.setQueueCapacity(5); pool.setThreadFactory( new ThreadFactoryBuilder().setDaemon(false).setNameFormat("kafka-topic1-task-pool-%d").build()); pool.initialize(); pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); return pool; } @Bean public Executor kafkaTopic2TaskExecPool() { ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor(); pool.setCorePoolSize(1); pool.setQueueCapacity(5); pool.setMaxPoolSize(1); pool.setThreadFactory( new ThreadFactoryBuilder().setDaemon(false).setNameFormat("kafka-topic2-task-pool-%d").build()); pool.initialize(); pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); return pool; } } 解释下这个线程池会失效吗
最新发布
11-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值