Spring線程池ThreadPoolTaskExecutor配置及詳情

Spring線程池ThreadPoolTaskExecutor配置及詳情

1. ThreadPoolTaskExecutor配置

 1 <!-- spring thread pool executor -->           
 2     <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
 3         <!-- 線程池維護線程的最少數量 -->
 4         <property name="corePoolSize" value="5" />
 5         <!-- 容許的空閒時間 -->
 6         <property name="keepAliveSeconds" value="200" />
 7         <!-- 線程池維護線程的最大數量 -->
 8         <property name="maxPoolSize" value="10" />
 9         <!-- 緩存隊列 -->
10         <property name="queueCapacity" value="20" />
11         <!-- 對拒絕task的處理策略 -->
12         <property name="rejectedExecutionHandler">
13             <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
14         </property>
15     </bean>

屬性字段說明java

corePoolSize:線程池維護線程的最少數量spring

keepAliveSeconds:容許的空閒時間緩存

maxPoolSize:線程池維護線程的最大數量多線程

queueCapacity:緩存隊列併發

rejectedExecutionHandler:對拒絕task的處理策略app

2. execute(Runable)方法執行過程

若是此時線程池中的數量小於corePoolSize,即便線程池中的線程都處於空閒狀態,也要建立新的線程來處理被添加的任務。ide

若是此時線程池中的數量等於 corePoolSize,可是緩衝隊列 workQueue未滿,那麼任務被放入緩衝隊列。this

若是此時線程池中的數量大於corePoolSize,緩衝隊列workQueue滿,而且線程池中的數量小於maxPoolSize,建新的線程來處理被添加的任務。spa

若是此時線程池中的數量大於corePoolSize,緩衝隊列workQueue滿,而且線程池中的數量等於maxPoolSize,那麼經過handler所指定的策略來處理此任務。也就是:處理任務的優先級爲:核心線程corePoolSize、任務隊列workQueue、最大線程 maximumPoolSize,若是三者都滿了,使用handler處理被拒絕的任務。線程

當線程池中的線程數量大於corePoolSize時,若是某線程空閒時間超過keepAliveTime,線程將被終止。這樣,線程池能夠動態的調整池中的線程數。

3. 示例代碼

Junit Test

 1 @RunWith(SpringJUnit4ClassRunner.class)
 2 @ContextConfiguration(classes = { MultiThreadConfig.class })
 3 public class MultiThreadTest {
 4 
 5     @Autowired
 6     private ThreadPoolTaskExecutor taskExecutor;
 7 
 8     @Autowired
 9     private MultiThreadProcessService multiThreadProcessService;
10     
11     @Test
12     public void test() {
13         
14         int n = 20;
15         for (int i = 0; i < n; i++) {
16             taskExecutor.execute(new MultiThreadDemo(multiThreadProcessService));
17             System.out.println("int i is " + i + ", now threadpool active threads totalnum is " + taskExecutor.getActiveCount());
18         }
19         
20         try {
21             System.in.read();
22         } catch (IOException e) {
23             throw new RuntimeException(e);
24         }
25     }
26 }

MultiThreadDemo

 1 /**
 2  * 多線程併發處理demo
 3  * @author daniel.zhao
 4  *
 5  */
 6 public class MultiThreadDemo implements Runnable {
 7 
 8     private MultiThreadProcessService multiThreadProcessService;
 9     
10     public MultiThreadDemo() {
11     }
12     
13     public MultiThreadDemo(MultiThreadProcessService multiThreadProcessService) {
14         this.multiThreadProcessService = multiThreadProcessService;
15     }
16     
17     @Override
18     public void run() {
19         multiThreadProcessService.processSomething();
20     }
21 
22 }

MultiThreadProcessService

 1 @Service
 2 public class MultiThreadProcessService {
 3 
 4     public static final Logger logger = Logger.getLogger(MultiThreadProcessService.class);
 5     
 6     /**
 7      * 默認處理流程耗時1000ms
 8      */
 9     public void processSomething() {
10         logger.debug("MultiThreadProcessService-processSomething" + Thread.currentThread() + "......start");
11         try {
12             Thread.sleep(1000);
13         } catch (InterruptedException e) {
14             throw new RuntimeException(e);
15         }
16         logger.debug("MultiThreadProcessService-processSomething" + Thread.currentThread() + "......end");
17     }
18 }

MultiThreadConfig

1 @Configuration
2 @ComponentScan(basePackages = { "com.xxx.multithread" })
3 @ImportResource(value = { "classpath:config/application-task.xml" })
4 @EnableScheduling
5 public class MultiThreadConfig {
6 }
相關文章
相關標籤/搜索