/** * 当线程池耗尽时,由调用者负责执行任务,并打印相关日志 */ @Slf4j public class CallerRunsWithLogPolicy implements RejectedExecutionHandler { public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { // shutdown():不会立即终止线程池,而是要等所有任务缓存队列中的任务都执行完后才终止,但再也不会接受新的任务 if (!e.isShutdown()) { log.error("线程池耗尽,本任务由调用方执行!"); r.run(); } } }
2、子线程打印主线程的日志信息
public class MdcContextDecorator implements TaskDecorator { @Override public Runnable decorate(@NonNull Runnable runnable) { Map<String, String> contextMap = MDC.getCopyOfContextMap(); return () -> { try { if (contextMap != null) { MDC.setContextMap(contextMap); } runnable.run(); } finally { MDC.clear(); } }; } }
标签:Runnable,MDC,contextMap,拒接,线程,日志,public From: https://www.cnblogs.com/yk775879106/p/17687167.html