我正在使用 Vertx 和 Log4j2。
这是代码Remote.java
public class Remote extends AbstractVerticle {
@Override
public void start() throws Exception {
System.setProperty("isThreadContextMapInheritable", "true");
ThreadContext.put("1", "Company");
ThreadContext.put("2", "Sector");
ThreadContext.put("3", "Address");
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 1; i < 4; i++) {
arrayList.add(String.valueOf(i));
}
arrayList.parallelStream().forEach(s -> {
System.out.println("Key " + s + " Value is " + ThreadContext.get(s) + " Name: " + Thread.currentThread().getName());
});
System.out.println("After parallelStream");
}
}
这是我的代码StartVerticle.java
public class StartVerticle {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new Remote());
}
}
当我运行上面的代码时,我的输出是
Key 2 Value is Sector Name: vert.x-eventloop-thread-0
Key 1 Value is null Name: ForkJoinPool.commonPool-worker-3
Key 3 Value is null Name: ForkJoinPool.commonPool-worker-5
After parallelStream
您能帮我获得以下输出吗?
Key 2 Value is Sector Name: vert.x-eventloop-thread-0
Key 1 Value is Company Name: ForkJoinPool.commonPool-worker-3
Key 3 Value is Address Name: ForkJoinPool.commonPool-worker-5
After parallelStream
我还打印了线程名称以供参考。在不使用 Vertx 的情况下,我可以ThreadContext
在设置isThreadContextMapInheritable
为true
.