This commit is contained in:
2023-03-05 17:53:40 +08:00
parent 9a09b5443a
commit 42d587f331
3 changed files with 282 additions and 15 deletions

View File

@ -131,7 +131,7 @@ public class MDCRunnable implements Runnable {
接着我们需要对main函数里创建的Runnable实现类进行装饰
```
```java
public class Main {
private static final String KEY = "requestId";
@ -140,13 +140,13 @@ public class Main {
public static void main(String[] args) {
*// 入口传入请求ID*
*MDC.put(KEY, UUID.randomUUID().toString());*
*
// 主线程打印日志*
*logger.debug(*"log in main thread");
// 入口传入请求ID
MDC.put(KEY, UUID.randomUUID().toString());
*// 异步线程打印日志用MDCRunnable装饰Runnable*
// 线程打印日志
logger.debug(*"log in main thread");
// 异步线程打印日志用MDCRunnable装饰Runnable
new Thread(new MDCRunnable(new Runnable() {
@Override
public void run() {
@ -154,8 +154,8 @@ public class Main {
}
})).start();
*// 异步线程池打印日志用MDCRunnable装饰Runnable*
*EXECUTOR.execute(*new MDCRunnable(new Runnable() {
// 异步线程池打印日志用MDCRunnable装饰Runnable
EXECUTOR.execute(*new MDCRunnable(new Runnable() {
@Override
public void run() {
logger.debug("log in other thread pool");
@ -163,12 +163,12 @@ public class Main {
}));
EXECUTOR.shutdown();
*// 出口移除请求ID*
*MDC.remove(KEY);*
*
}*
*
}*
// 出口移除请求ID
MDC.remove(KEY);
}
}
```