ThreadPoolExecutor类

最近的高频任务,自己写的用到了这个类.其实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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章