直接上代码:
线程池相关配置:这些数据可以根据自己业务需要进行调整
#threadpool
#核心线程数
threadpool.corePoolSize=8
#最大线程数
threadpool.maxPoolSize=200
#队列容量
threadpool.queueCapacity=9999
#线程活跃时间(秒)
threadpool.keepAliveSeconds=300
1、线程池配置实体
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* Description: 线程池配置
*
* @author w.y.s
* @date 2020/8/17 15:06
*/
@Configuration
@ConfigurationProperties(prefix = "threadpool")
public class ThreadPoolConfig {
/**
* 核心线程数
*/
private Integer corePoolSize;
/**
* 最大线程数
*/
private Integer maxPoolSize;
/**
* 队列容量
*/
private Integer queueCapacity;
/**
* 线程活跃时间(秒)
*/
private Integer keepAliveSeconds;
public Integer getCorePoolSize() {
return corePoolSize;
}
public void setCorePoolSize(Integer corePoolSize) {
this.corePoolSize = corePoolSize;
}
public Integer getMaxPoolSize() {
return maxPoolSize;
}
public void setMaxPoolSize(Integer maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
public Integer getQueueCapacity() {
return queueCapacity;
}
public void setQueueCapacity(Integer queueCapacity) {
this.queueCapacity = queueCapacity;
}
public Integer getKeepAliveSeconds() {
return keepAliveSeconds;
}
public void setKeepAliveSeconds(Integer keepAliveSeconds) {
this.keepAliveSeconds = keepAliveSeconds;
}
}
2、线程池创建管理
/**
* Description: 线程池配置
*
* @author w.y.s
* @date 2020/8/17 13:31
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class ThreadPooTasklExecutor {
@Autowired
ThreadPoolConfig threadPoolConfig;
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(threadPoolConfig.getCorePoolSize());
executor.setMaxPoolSize(threadPoolConfig.getMaxPoolSize());
executor.setQueueCapacity(threadPoolConfig.getQueueCapacity());
executor.setKeepAliveSeconds(threadPoolConfig.getKeepAliveSeconds());
// 设置默认线程名称
executor.setThreadNamePrefix("thread-");
// 设置拒绝策略rejection-policy:当pool已经达到max size的时候,如何处理新任务 CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
}
3、测试使用线程池实例:
// 1)注入线程池
/**
* 线程池 taskExecutor
*/
@Resource(name = "taskExecutor")
TaskExecutor taskExecutor;
//2)使用线程池
private void test() {
// 执行线程
taskExecutor.execute(new TestThread("test", contest, testService));
}
4、线程实例
public class TestThread implements Runnable {
private String title;
private String context;
@Autowired
private TestService testService;
public TestThread (String title, String context, TestService testService) {
this.subject = title;
this.context = context;
this.TestService= testService;
}
@Override
public void run() {
// 具体的业务逻辑实现
testService.sendMessage(message);
}
}