This article introduces the configuration of the Spring thread pool ThreadPoolTaskExecutor, and shares it with you, as follows:
1. ThreadPoolTaskExecutor configuration
<!-- spring thread pool executor --> <bean id="taskExecutor"> <!-- Minimum number of threads maintained by thread pool --> <property name="corePoolSize" value="5" /> <!-- Allowed idle time --> <property name="keepAliveSeconds" value="200" /> <!-- Maximum number of threads maintained by thread pool --> <property name="maxPoolSize" value="10" /> <!-- Cache Queue--> <property name="queueCapacity" value="20" /> <!-- Handling policy for rejecting tasks --> <property name="rejectedExecutionHandler"> <bean /> </property> </bean>
Attribute field description
corePoolSize: The minimum number of threads maintained by thread pool
keepAliveSeconds: Allowed free time
maxPoolSize: The maximum number of threads maintained by thread pool
queueCapacity: cache queue
rejectedExecutionHandler: Handling strategy for rejecting task
2. Execute(Runable) method execution process
If the number of thread pools is less than corePoolSize at this time, even if the threads in the thread pool are all idle, a new thread must be created to handle the added tasks.
If the number in the thread pool is equal to corePoolSize, but the buffer queue workQueue is not full, then the task is placed in the buffer queue.
If the number of thread pools is greater than corePoolSize at this time, the buffer queue workQueue is full, and the number of thread pools is less than maxPoolSize, create a new thread to handle the added tasks.
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 this task is handled through the policy specified by the handler. That is, the priority of handling tasks is: core thread corePoolSize, task queue workQueue, maximum thread maximumPoolSize. If all three are full, use handler to handle the rejected task.
When the number of threads in the thread pool is greater than corePoolSize, if a thread is idle for more than 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
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = { MultiThreadConfig.class })public class MultiThreadTest { @Autowired private ThreadPoolTaskExecutor taskExecutor; @Autowired private MultiThreadProcessService multiThreadProcessService; @Test public void test() { int n = 20; for (int i = 0; i < n; i++) { taskExecutor.execute(new MultiThreadDemo(multiThreadProcessService)); System.out.println("int i is " + i + ", now threadpool active threads totalnum is " + taskExecutor.getActiveCount()); } try { System.in.read(); } catch (IOException e) { throw new RuntimeException(e); } }}MultiThreadDemo
/** * Multithread concurrent processing demo * @author daniel.zhao * */public class MultiThreadDemo implements Runnable { private MultiThreadProcessService multiThreadProcessService; public MultiThreadDemo() { } public MultiThreadDemo(MultiThreadProcessService multiThreadProcessService) { this.multiThreadProcessService = multiThreadProcessService; } @Override public void run() { multiThreadProcessService.processSomething(); }}MultiThreadProcessService
@Servicepublic class MultiThreadProcessService { public static final Logger logger = Logger.getLogger(MultiThreadProcessService.class); /** * The default processing process takes 1000ms */ public void processSomething() { logger.debug("MultiThreadProcessService-processSomething" + Thread.currentThread() + "......start"); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } logger.debug("MultiThreadProcessService-processSomething" + Thread.currentThread() + "......end"); }}MultiThreadConfig
@Configuration @ComponentScan(basePackages = { "com.xxx.multithread" }) @ImportResource(value = { "classpath:config/application-task.xml" }) @EnableScheduling public class MultiThreadConfig { }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.