@@ -0,0 +1,48 @@
|
||||
package top.crushtj.xiaoyishu.auth.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
import static top.crushtj.xiaoyishu.auth.constant.ConfigConstants.*;
|
||||
|
||||
/**
|
||||
* @author ayi
|
||||
* @version V1.0
|
||||
* @title ThreadPoolConfig
|
||||
* @date 2026/01/16 22:26
|
||||
* @description 线程池配置类
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class ThreadPoolConfig {
|
||||
|
||||
@Bean(name = "taskExecutor")
|
||||
public Executor taskExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
// 核心线程数
|
||||
executor.setCorePoolSize(CORE_POOL_SIZE);
|
||||
// 最大线程数
|
||||
executor.setMaxPoolSize(MAX_POOL_SIZE);
|
||||
// 队列容量
|
||||
executor.setQueueCapacity(QUEUE_CAPACITY);
|
||||
// 线程活跃时间(秒)
|
||||
executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
|
||||
// 线程名前缀
|
||||
executor.setThreadNamePrefix(THREAD_NAME_PREFIX);
|
||||
|
||||
// 拒绝策略:由调用线程处理(一般为主线程)
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
|
||||
// 等待所有任务结束后再关闭线程池
|
||||
executor.setWaitForTasksToCompleteOnShutdown(true);
|
||||
// 设置等待时间,如果超过这个时间还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是被没有完成的任务阻塞
|
||||
executor.setAwaitTerminationSeconds(AWAIT_TERMINATION_SECONDS);
|
||||
|
||||
executor.initialize();
|
||||
return executor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package top.crushtj.xiaoyishu.auth.constant;
|
||||
|
||||
/**
|
||||
* @author ayi
|
||||
* @version V1.0
|
||||
* @title ConfigConstants
|
||||
* @date 2026/01/16
|
||||
* @description 配置常量类
|
||||
*/
|
||||
|
||||
public class ConfigConstants {
|
||||
|
||||
/**
|
||||
* 核心线程数
|
||||
*/
|
||||
public static final int CORE_POOL_SIZE = 10;
|
||||
/**
|
||||
* 最大线程数
|
||||
*/
|
||||
public static final int MAX_POOL_SIZE = 50;
|
||||
/**
|
||||
* 队列容量
|
||||
*/
|
||||
public static final int QUEUE_CAPACITY = 200;
|
||||
/**
|
||||
* 线程活跃时间(秒)
|
||||
*/
|
||||
public static final int KEEP_ALIVE_SECONDS = 30;
|
||||
/**
|
||||
* 线程名前缀
|
||||
*/
|
||||
public static final String THREAD_NAME_PREFIX = "AuthExecutor-";
|
||||
|
||||
public static final int AWAIT_TERMINATION_SECONDS = 60;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package top.crushtj.xiaoyishu.auth;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
/**
|
||||
* @author ayi
|
||||
* @version V1.0
|
||||
* @title ThreadPoolTaskExecutorTests
|
||||
* @date 2026/01/16 22:34
|
||||
* @description 线程池测试类
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@SpringBootTest
|
||||
public class ThreadPoolTaskExecutorTests {
|
||||
@Resource
|
||||
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
||||
|
||||
/**
|
||||
* 测试线程池
|
||||
*/
|
||||
@Test
|
||||
void testSubmit() {
|
||||
int count = 300;
|
||||
while (count-- > 0) {
|
||||
int finalCount = count;
|
||||
threadPoolTaskExecutor.submit(() -> log.info("异步线程: 这是{}号线程", finalCount));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user