spring thread pool

Configured in applicationContext.xml
<!--==========Thread pool configuration start========================-->
<bean id ="threadPoolTaskExecutor"
 class ="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
 <!-- The number of core threads, the default is 1 -->
 <property name ="corePoolSize" value ="8" />
 < !-- The maximum number of threads, the default is Integer.MAX_VALUE -->
 <property name ="maxPoolSize" value ="10" />
 <!-- The maximum length of the queue, generally need to set the value>=notifyScheduledMainExecutor.maxNum; the default is Integer .MAX_VALUE-->
 <property name ="queueCapacity" value ="200" />
 <!-- The idle time allowed by the thread pool maintenance thread, the default is 60s -->
<property name="keepAliveSeconds" value = "300" />
 <!-- The thread pool's processing strategy for rejected tasks (no threads available), currently only supports AbortPolicy and CallerRunsPolicy; the default is the latter -->
 <property name = "rejectedExecutionHandler" >
 <!- - AbortPolicy: java.util.concurrent.RejectedExecutionException is thrown directly -->
       <!-- CallerRunsPolicy: The main thread executes the task directly. After the execution, try to add the next task to the thread pool, which can effectively reduce the number of tasks added to the thread pool. The speed of the task -->
       <!-- DiscardOldestPolicy: discard the old task, temporarily not supported; will cause the discarded task to be unable to be executed again -->
       <!-- DiscardPolicy: discard the current task, temporarily not supported; will cause Abandoned tasks cannot be executed again -->
 <bean class ="java.util.concurrent.ThreadPoolExecutor$AbortPolicy" />
 <!--<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />- ->
 <
/property>
</bean>

test code
ApplicationContext ac = new ClassPathXmlApplicationContext("config/spring/applicationContext-mini.xml");
ThreadPoolTaskExecutor threadPoolTaskExecutor=(ThreadPoolTaskExecutor)ac.getBean("threadPoolTaskExecutor");
for (int i = 1; i <= 11; i++) {
    String d= DateTimeUtil.getNowDate4 () ;
 System.out .println ( "The " + i+ " task starts executing #" +d) ;
 threadPoolTaskExecutor.execute( new Runnable() {
         public void run () {
            String d1= DateTimeUtil.getNowDate4 () ;
 //Write processing business code here
 String msg= RedisUtil.lpop ( " test0714 " ) ;
 System.out .println ( "##########" +msg+ "Task Processing..." +d1) ;
             try {
                Thread.sleep(2000);
} catch (InterruptedException e) {
                e.printStackTrace () ;
}


        }
    });
}


The relationship between corePoolSize, maxPoolSize, and queueCapacity

corePoolSize the number of core threads, the system can start up to several threads to run at a time

maxPoolSize maximum number of threads, the system can run up to several threads to run

queueCapacity The number of buffer queues, how many threads the system can buffer at most

Example:

corePoolSize=8;maxPoolSize=10;queueCapacity=20;
Join now the thread pool is idle
40 concurrent requests came over at a time
When threadPoolTaskExecutor.execute is called, 8 threads are executed in parallel, and the remaining 32 enter the
In the queueCapacity queue, but the buffer queue can only buffer 20, at this time, the maximum number of threads still has two places.
2 more threads will be created to execute the request. 
A total of requests that can be processed at this time = 8+20+2=30 requests
When the threads in corePoolSize are processed, new processing requirements will be taken from the queueCapacity queue and entered into
Medium corePoolSize.

Guess you like

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