ThreadPoolExecutor类

本文详细介绍了如何在Spring框架中配置线程池,包括核心线程池大小、最大线程池大小等参数的设置,并提供了具体的配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近的高频任务,自己写的用到了这个类.其实spring框架里面这个类肯定用到.以前没有仔细看.

这个类在spring框架中用到线程池的时候用到的.他的用法:

ThreadPoolExecutor executor  = new ThreadPoolExecutor(
                this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS,
                queue, threadFactory, rejectedExecutionHandler);

corePoolSize,核心线程池大小

maxPoolSize 最大扩展池大小

keepAliveSeconds, 空闲时间

TimeUnit.SECONDS 这个是线程池计时单位

queue, 排队队列

threadFactory:

要用的时候,如下:

<bean id="hftMainTaskThreadPool"
        class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="2" />
        <property name="maxPoolSize" value="10" />
        <property name="queueCapacity" value="2" />
        <property name="keepAliveSeconds" value="300" />
        <property name="rejectedExecutionHandler">
            <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
        </property>
    </bean>

然后再结合

MethodUtils.invokeExactMethod(bean, beansmethod, object[]args);

在调用的时候利用spring的注入,

怎么注入: bean中注入这个threadpool, 调用的beanmethod.这时候,是一个线程执行这个bean的method

然后,多个bean执行他的method.

spring的多线程就是这个套路.为什么这么说因为这个ThreadPoolTaskExecutor就是

org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值