Commit f4fb890f authored by alex yao's avatar alex yao

feat(threadpool): 添加公共线程池异常处理和优化配置

- 新增 CommonThreadPoolExceptionHandler 类处理线程池任务异常
- 实现 Thread.UncaughtExceptionHandler 接口记录异常日志并抛出业务异常
- 配置线程池核心参数:核心线程数CPU核数,最大线程数CPU核数*20,队列大小CPU核数*2
- 设置拒绝策略为 CallerRunsPolicy 并添加线程池名称和守护线程配置
- 将 execute 方法改为 submit 方法提交任务
- 添加 getThreadPoolExecutor 方法获取线程池实例
- 在ThreadFactory中设置异常处理器
parent eb5ddd75
package cn.com.poc.common.pool;
import cn.com.yict.framemax.core.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 公共线程池,任务异常处理
*
* @author alex.yao
* @date 2026/1/15
*/
public class CommonThreadPoolExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(CommonThreadPoolExceptionHandler.class);
@Override
public void uncaughtException(Thread t, Throwable e) {
logger.error("线程:{} , 线程池任务执行异常: {}", t.getName(), e.getMessage(), e);
throw new BusinessException(e.getMessage());
}
}
...@@ -4,11 +4,19 @@ import cn.com.yict.framemax.core.i18n.I18nMessageException; ...@@ -4,11 +4,19 @@ import cn.com.yict.framemax.core.i18n.I18nMessageException;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
/** /**
* 公共业务线程池 * 公共业务线程池
*
* 核心线程数:CPU核数
* 最大线程数:CPU核数*20
* 队列大小:CPU核数*2
* 拒绝策略:CallerRunsPolicy
* 线程池名称:Common_Thread_Executor
* 是否为守护线程: false
*/ */
public class CommonThreadPoolExecutor { public class CommonThreadPoolExecutor {
...@@ -22,24 +30,42 @@ public class CommonThreadPoolExecutor { ...@@ -22,24 +30,42 @@ public class CommonThreadPoolExecutor {
final private static LinkedBlockingQueue<Runnable> WORK_QUEUE = new LinkedBlockingQueue<>(CORE_POOL_SIZE * 2); final private static LinkedBlockingQueue<Runnable> WORK_QUEUE = new LinkedBlockingQueue<>(CORE_POOL_SIZE * 2);
final private static ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(THREAD_POOL_NAME + "-%d").setDaemon(false).build(); final private static Thread.UncaughtExceptionHandler EXCEPTION_HANDLER = new CommonThreadPoolExceptionHandler();
private static ThreadPoolExecutor THREAD_POOL_EXECUTOR; final private static RejectedExecutionHandler REJECTED_EXECUTION_HANDLER =
new ThreadPoolExecutor.CallerRunsPolicy();
static { final private static ThreadFactory THREAD_FACTORY =
initThreadPool(); new ThreadFactoryBuilder()
} .setUncaughtExceptionHandler(EXCEPTION_HANDLER)
.setNameFormat(THREAD_POOL_NAME + "-%d")
.setDaemon(false)
.build();
final private static ThreadPoolExecutor THREAD_POOL_EXECUTOR;
private static void initThreadPool() { static {
THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, java.util.concurrent.TimeUnit.SECONDS, WORK_QUEUE, THREAD_FACTORY, new ThreadPoolExecutor.CallerRunsPolicy()); THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
CORE_POOL_SIZE,
MAXIMUM_POOL_SIZE,
KEEP_ALIVE_TIME,
java.util.concurrent.TimeUnit.SECONDS,
WORK_QUEUE,
THREAD_FACTORY,
REJECTED_EXECUTION_HANDLER
);
} }
public static void addTask(Runnable task) { public static void addTask(Runnable task) {
try { try {
THREAD_POOL_EXECUTOR.execute(task); THREAD_POOL_EXECUTOR.submit(task);
} catch (Exception e) { } catch (Exception e) {
throw new I18nMessageException("exception/there.are.too.many.tasks.currently.please.try.again.later"); throw new I18nMessageException("exception/there.are.too.many.tasks.currently.please.try.again.later");
} }
} }
public static ThreadPoolExecutor getThreadPoolExecutor() {
return THREAD_POOL_EXECUTOR;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment