最近的高頻任務,自己寫的用到了這個類.其實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