InheritableThreadLocalDemo

This commit is contained in:
laker
2022-11-10 17:09:54 +08:00
parent 600622e9f8
commit 423b87c9c8
3 changed files with 4 additions and 3 deletions

View File

@@ -39,7 +39,7 @@ import java.util.concurrent.TimeUnit;
* parentThreadName:pool-1-thread-9:8 pool-2-thread-4
* </pre>
*/
public class InheritableThreadLocalDemoTrace2 {
public class InheritableThreadLocalDemoTraceCopy {
/**
* 模拟tomcat线程池
*/

View File

@@ -48,10 +48,11 @@ public class TraceCopyThreadPoolExecutor extends EasyAdminThreadPoolExecutor {
Trace parentValue = TraceContext.trace();
return () -> {
if (parentValue == null) {
log.info("ThreadLocal GToadContext is initialized");
TraceContext.setTrace(new Trace());
} else {
// 复制 而不是引用
log.trace("ThreadLocal childValue is initialized, parent: {}", parentValue);
log.info("ThreadLocal childValue is initialized, parent: {}", parentValue);
Trace trace = new Trace();
trace.setDepth(parentValue.getDepth());
trace.setSpans(parentValue.getSpans());