Created
May 19, 2020 07:50
-
-
Save kojilin/a7012ec94f22014c383de4885b9d3133 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static ThreadLocal<String> threadLocal = new InheritableThreadLocal<>(); | |
public static void main(String[] args) throws InterruptedException { | |
threadLocal.set("monamona"); | |
Thread t1 = Thread.builder().inheritThreadLocals().virtual().task(() -> { | |
System.out.println("foo pre:" + threadLocal.get()); | |
threadLocal.set("foo"); | |
foo(); | |
}).start(); | |
Thread t2 = Thread.startVirtualThread(() -> { | |
System.out.println("bar pre:" + threadLocal.get()); | |
threadLocal.set("bar"); | |
foo(); | |
}); | |
Thread t3 = Thread.builder().task(() -> { | |
System.out.println("hoge pre:" + threadLocal.get()); | |
threadLocal.set("hoge"); | |
foo(); | |
}).start(); | |
Thread t4 = new Thread(() -> { | |
System.out.println("t4 pre:" + threadLocal.get()); | |
threadLocal.set("t4"); | |
foo(); | |
}); | |
t4.start(); | |
t1.join(); | |
t2.join(); | |
t3.join(); | |
t4.join(); | |
} | |
static void foo() { | |
bar(); | |
} | |
static void bar() { | |
try { | |
Thread.sleep(3000); | |
throw new RuntimeException("hogehoge:" + threadLocal.get()); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hoge pre:null | |
t4 pre:monamona | |
foo pre:monamona | |
bar pre:null | |
Exception in thread "Thread-1" Exception in thread "Thread-0" java.lang.RuntimeException: hogehoge:t4 | |
at com.example.Main.bar(Main.java:46) | |
at com.example.Main.foo(Main.java:40) | |
at com.example.Main.lambda$main$3(Main.java:28) | |
at java.base/java.lang.Thread.run(Thread.java:1676) | |
java.lang.RuntimeException: hogehoge:hoge | |
at com.example.Main.bar(Main.java:46) | |
at com.example.Main.foo(Main.java:40) | |
at com.example.Main.lambda$main$2(Main.java:22) | |
at java.base/java.lang.Thread.run(Thread.java:1676) | |
Exception in thread "<unnamed>" Exception in thread "<unnamed>" java.lang.RuntimeException: hogehoge:foo | |
at com.example.Main.bar(Main.java:46) | |
at com.example.Main.foo(Main.java:40) | |
at com.example.Main.lambda$main$0(Main.java:11) | |
at java.base/java.lang.VirtualThread.lambda$new$0(VirtualThread.java:122) | |
at java.base/java.lang.Continuation.enter(Continuation.java:380) VirtualThreads | |
at java.base/java.lang.Continuation.run(Continuation.java:337) | |
at java.base/java.lang.VirtualThread.runContinuation(VirtualThread.java:206) | |
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1429) | |
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) | |
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1018) | |
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1667) | |
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1600) | |
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:189) | |
java.lang.RuntimeException: hogehoge:bar | |
at com.example.Main.bar(Main.java:46) | |
at com.example.Main.foo(Main.java:40) | |
at com.example.Main.lambda$main$1(Main.java:16) | |
at java.base/java.lang.VirtualThread.lambda$new$0(VirtualThread.java:122) | |
at java.base/java.lang.Continuation.enter(Continuation.java:380) VirtualThreads | |
at java.base/java.lang.Continuation.run(Continuation.java:337) | |
at java.base/java.lang.VirtualThread.runContinuation(VirtualThread.java:206) | |
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1429) | |
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) | |
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1018) | |
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1667) | |
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1600) | |
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:189) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment