Spring thread pool ThreadPoolTaskExecutor configuration and details

1. ThreadPoolTaskExecutor配置

copy code
1  <!-- spring thread pool executor -->            
2      < bean id = "taskExecutor" class = "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" > 
3          <!-- The minimum number of threads for thread pool maintenance --> 
4          < property name ="corePoolSize" value ="5"  /> 
5          <!-- allowable idle time --> 
6          < property name ="keepAliveSeconds" value ="200"  /> 
7          <!--The maximum number of thread pool maintenance threads --> 
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>
copy code

Property Field Description

corePoolSize: The minimum number of thread pool maintenance threads

keepAliveSeconds: Allowed idle time

maxPoolSize: The maximum number of thread pool maintenance threads

queueCapacity: cache queue

rejectedExecutionHandler: Processing strategy for rejected tasks

2. execute(Runable) method execution process

If the number of threads in the thread pool is smaller than corePoolSize at this time, even if the threads in the thread pool are all idle, new threads are created to process the added tasks.

If the number in the thread pool is equal to  corePoolSize at this time, but the buffer queue workQueue is not full, then the task is put into the buffer queue.

If the number in the thread pool is greater than corePoolSize at this time, the buffer queue workQueue is full, and the number in the thread pool is less than maxPoolSize, a new thread is created to process the added task.

If the number in the thread pool is greater than corePoolSize at this time, the buffer queue workQueue is full, and the number in the thread pool is equal to maxPoolSize, then the task is processed by the strategy specified by the handler. That is: the priority of processing tasks is: core thread corePoolSize, task queue workQueue, maximum thread maximumPoolSize, if all three are full, use handler to process rejected tasks.

When the number of threads in the thread pool is greater than corePoolSize, if a thread idle time exceeds keepAliveTime, the thread will be terminated. In this way, the thread pool can dynamically adjust the number of threads in the pool.

3. Sample code

Junit Test

copy code
 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 }
copy code

MultiThreadDemo

copy code
 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 }
copy code

MultiThreadProcessService

copy code
 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 }
copy code

MultiThreadConfig

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

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326739483&siteId=291194637