线程池优化
前言
博主公司的项目一直有一个问题 ,跑时间长了会非常卡顿 ,必须要重启才能解决任务 ,之前没空排查问题 ,现在终于有时间来排查和解决这个问题了 。
打印jvm的dump信息辅助排查
先找到进程id:jps
打印堆栈信息输出到文件:jsatck -l [进程id] > /tmp/jvm.dump
分析堆栈信息
因为讲求的是快速排查 ,所以也就不下载软件了,直接使用oline版的fastThread来进行分析 。
https://fastthread.io/ft-dashboard.jsp
网页进去直接上传文件

之后就会生成报告

进入后就可以看到详细信息:
目录:

线程概览(总线程数)

线程池
会根据线程名称聚合线程池数量

守护进程和非守护进程

。。。后面的可以自己看
目前博主的问题已经找到了 ,不断创建新线程可能会导致OOM,先优化这个问题。
配置全局线程池
线程池配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class ThreadPoolConfig {
@Value("${long.pool.corePoolSize:32}")
private int threadPoolCorePoolSize;
@Value("${long.pool.maxPoolSize:64}")
private int threadPoolMaxPoolSize;
@Value("${long.pool.queueCapacity:500}")
private int threadPoolQueueCapacity;
@Value("${long.pool.KeepAliveSeconds:500}")
private int threadPoolKeepAliveSeconds;
@Value("${long.pool.threadNamePrefix:long-ThreadPool-}")
private String threadNamePrefix;
/**
* 主业务线程池
*/
@Primary
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(threadPoolCorePoolSize);
executor.setMaxPoolSize(threadPoolMaxPoolSize);
executor.setQueueCapacity(threadPoolQueueCapacity);
executor.setKeepAliveSeconds(threadPoolKeepAliveSeconds);
executor.setThreadNamePrefix(threadNamePrefix);
executor.setAllowCoreThreadTimeOut(true);
//线程拒绝策略,不会丢弃任务 ,会让tomcat线程来执行 ,一定程度会影响web响应速度
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setAwaitTerminationSeconds(30);
executor.initialize();
return executor;
}
}延迟任务
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
public class DelayTaskScheduler {
private ScheduledExecutorService scheduler;
// 注入业务线程池
@Autowired
private ThreadPoolTaskExecutor executor;
@PostConstruct
public void init() {
// 调度线程池,只做任务调度
scheduler = Executors.newScheduledThreadPool(2, r -> new Thread(r, "long-Delay-Thread"));
}
@PreDestroy
public void shutdown() {
if (scheduler != null) {
scheduler.shutdown();
}
}
/** 延迟执行任务,自动提交给业务线程池 */
public void runAfterSeconds(int delaySeconds, Runnable task) {
scheduler.schedule(() -> executor.execute(task), delaySeconds, TimeUnit.SECONDS);
}
/** 延迟 60 秒任务 */
public void runAfter60Seconds(Runnable task) {
runAfterSeconds(60, task);
}
}@Async注解
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
* @ProjectName: 六期
* @Package: com.sinoprof.mallcommon.config
* @ClassName:
* @Description: springboot异步线程配置 // 类功能简述
* @Author: JingLonglong // 作者
* @Date: 2025/11/28 // 创建日期
* @Version: 1.0
*/
@Configuration
public class AsyncConfig {
// 可以直接复用全局线程池
@Bean(name = "taskExecutor")
public Executor taskExecutor(ThreadPoolTaskExecutor threadPool) {
// 使用你的全局线程池
return threadPool;
}
}调用示例
@Autowired
private DelayTaskScheduler delayTask;
@Autowired
private ThreadPoolTaskExecutor threadPool;
//CompletableFuture调用
CompletableFuture.runAsync(() -> {
ShardingUtil.setDpByTenantId(faceAuthResult.getTenantId());
authBatchSignatrueBySim(entity, faceAuthResult, contractId, key);
}, threadPool);
//新启线程执行
taskExecutor.execute(() -> {
//...这里添加业务逻辑
});
//60s后异步执行
delayTask.runAfter60Seconds(() -> {
//...这里添加业务逻辑
});
License:
CC BY 4.0