Concurrent - thread pool

Please indicate the source of the original reprint: http://agilestyle.iteye.com/blog/2390819

 

There are four predefined Reject strategies: 

(1) ThreadPoolExecutor.AbortPolicy policy, which is the default policy, will throw RejectedExecutionException at runtime if the handler is rejected. 

(2) ThreadPoolExecutor.CallerRunsPolicy strategy, the caller's thread will execute the task, and if the executor is closed, it will be discarded. 

(3) ThreadPoolExecutor.DiscardPolicy strategy, tasks that cannot be executed will be discarded. 

(4) ThreadPoolExecutor.DiscardOldestPolicy policy, if the executor has not been closed, the task at the head of the work queue will be deleted, and then the executor will be retried (if it fails again, this process will be repeated).

 

The processing flow of the ThreadPoolExecutor executor: 

(1) When the thread pool size is less than corePoolSize, create a new thread and process the request. 

(2) When the thread pool size is equal to corePoolSize, put the request into the workQueue, and the idle threads in the pool will fetch tasks from the workQueue and process them. 

(3) When the workQueue cannot hold the newly entered task, the new thread joins the thread pool and processes the request. If the size of the pool reaches the maximumPoolSize, the RejectedExecutionHandler is used for rejection processing. 

(4) In addition, when the number of threads in the thread pool is greater than corePoolSize, the extra threads will wait for a long time for keepAliveTime, and if there is no request to process, they will be destroyed by themselves.

 

Example:

<bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
	<!-- The minimum number of thread pool maintenance threads-->
	<property name="corePoolSize" value="30"/>
	<!-- The maximum number of thread pool maintenance threads-->
	<property name="maxPoolSize" value="400"/>
	<!-- Cache queue -->
	<property name="queueCapacity" value="100"/>
	<!-- Allowed idle time -->
	<property name="keepAliveSeconds" value="200"/>
	<!-- Processing strategy for rejected tasks-->
	<property name="rejectedExecutionHandler">
		<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/>
	</property>
</bean>

 

Guess you like

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