当前位置:网站首页>Treadpoolconfig thread pool configuration in real projects

Treadpoolconfig thread pool configuration in real projects

2022-07-07 01:56:00 Novice Zhang~

package com.menglar.soap.item.common.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/** * @program: soap-item_v2 * @description:  Thread pool configuration  * @author: ZhangRiTian * @create: 2021-10-25 09:24 */

@Configuration
@EnableAsync
public class TreadPoolConfig {
    

	@Value("${threadpool.corePoolSize}")
	private int corePoolSize;

	@Value("${threadpool.maxPoolSize}")
	private int maxPoolSize;

	@Value("${threadpool.queueCapacity}")
	private int queueCapacity;

	@Value("${threadpool.keepAliveSeconds}")
	private int keepAliveSeconds;

	@Bean(name = "threadPoolTaskExecutor")
	public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
    
		ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
		// Number of core thread pools , Method :  Returns the number of available processors Java Number of virtual machines .
		pool.setCorePoolSize(corePoolSize);
		// Maximum number of threads 
		pool.setMaxPoolSize(maxPoolSize);
		// Queue capacity of the thread pool 
		pool.setQueueCapacity(queueCapacity);
		// The thread pool maintains the free time allowed for threads 
		pool.setKeepAliveSeconds(keepAliveSeconds);
		// Prefix of thread name 
		pool.setThreadNamePrefix("this-excutor-menglar-");
		// setRejectedExecutionHandler: When pool Already achieved max size When , How to deal with new tasks 
		// CallerRunsPolicy: Do not execute tasks in New Threads , Instead, it is executed by the thread of the caller 
		pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		pool.initialize();
		return pool;
	}
	/** *  Exception handling in asynchronous tasks  * */
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
    
		// TODO Auto-generated method stub
		return new SimpleAsyncUncaughtExceptionHandler();
	}
}

原网站

版权声明
本文为[Novice Zhang~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130911190107.html