Refactoring thread pool sample code

pull/218/head
chen.ma 3 years ago
parent 4e6100e55d
commit b281c150c1

@ -53,7 +53,6 @@ public class ThreadPoolConfig {
.awaitTerminationMillis(5000L) .awaitTerminationMillis(5000L)
.taskDecorator(new TaskTraceBuilderHandler()) .taskDecorator(new TaskTraceBuilderHandler())
.build(); .build();
return customExecutor; return customExecutor;
} }
@ -69,12 +68,10 @@ public class ThreadPoolConfig {
.waitForTasksToCompleteOnShutdown(true) .waitForTasksToCompleteOnShutdown(true)
.awaitTerminationMillis(5000L) .awaitTerminationMillis(5000L)
/** /**
* 线. * {@link TaskDecoratorTest}
* , {@link TaskDecoratorTest}
*/ */
.taskDecorator(new TaskDecoratorTest.ContextCopyingDecorator()) .taskDecorator(new TaskDecoratorTest.ContextCopyingDecorator())
.build(); .build();
return produceExecutor; return produceExecutor;
} }

@ -49,7 +49,5 @@ public class ErrorLogRejectedExecutionHandler implements CustomRejectedExecution
Logger logger = LoggerFactory.getLogger(this.getClass()); Logger logger = LoggerFactory.getLogger(this.getClass());
logger.error("线程池抛出拒绝策略."); logger.error("线程池抛出拒绝策略.");
} }
} }
} }

@ -34,7 +34,6 @@ public final class TaskTraceBuilderHandler implements TaskDecorator {
@Override @Override
public Runnable decorate(Runnable runnable) { public Runnable decorate(Runnable runnable) {
String executeTimeoutTrace = MDC.get(EXECUTE_TIMEOUT_TRACE); String executeTimeoutTrace = MDC.get(EXECUTE_TIMEOUT_TRACE);
Runnable taskRun = () -> { Runnable taskRun = () -> {
if (StringUtil.isNotBlank(executeTimeoutTrace)) { if (StringUtil.isNotBlank(executeTimeoutTrace)) {
MDC.put(EXECUTE_TIMEOUT_TRACE, executeTimeoutTrace); MDC.put(EXECUTE_TIMEOUT_TRACE, executeTimeoutTrace);
@ -42,8 +41,6 @@ public final class TaskTraceBuilderHandler implements TaskDecorator {
runnable.run(); runnable.run();
// 此处不用进行清理操作, 统一在线程任务执行后清理 // 此处不用进行清理操作, 统一在线程任务执行后清理
}; };
return taskRun; return taskRun;
} }
} }

@ -56,5 +56,4 @@ public class AlarmSendMessageTest {
} }
}, 3, 1, TimeUnit.SECONDS); }, 3, 1, TimeUnit.SECONDS);
} }
} }

@ -69,7 +69,6 @@ public class RunStateHandlerTest {
// 启动动态线程池模拟运行任务 // 启动动态线程池模拟运行任务
runTask(messageConsumeDynamicThreadPool); runTask(messageConsumeDynamicThreadPool);
// 启动动态线程池模拟运行任务 // 启动动态线程池模拟运行任务
runTask(messageProduceDynamicThreadPool); runTask(messageProduceDynamicThreadPool);
} }
@ -102,11 +101,8 @@ public class RunStateHandlerTest {
} catch (Exception ex) { } catch (Exception ex) {
// ignore // ignore
} }
ThreadUtil.sleep(500); ThreadUtil.sleep(500);
} }
}); });
} }
} }

@ -74,7 +74,6 @@ public class TaskDecoratorTest {
log.info("通过 taskDecorator MDC 传递上下文 :: {}", MDC.get(PLACEHOLDER)); log.info("通过 taskDecorator MDC 传递上下文 :: {}", MDC.get(PLACEHOLDER));
}); });
}); });
} }
public static class ContextCopyingDecorator implements TaskDecorator { public static class ContextCopyingDecorator implements TaskDecorator {
@ -93,5 +92,4 @@ public class TaskDecoratorTest {
}; };
} }
} }
} }

Loading…
Cancel
Save