InheritableThreadLocal的测试
package t1;
public class TestThread8 {
private static volatile InheritableThreadLocal<String> intVal = new InheritableThreadLocal<String>();
public static void main(String[] args) {
Runnable rP = () -> {
intVal.set(new String("10"));
Runnable rC = () -> {
Thread thd = Thread.currentThread();
String name = thd.getName();
System.out.printf("%s %s", name, intVal.get());
};
Thread thdChild = new Thread(rC);
thdChild.setName("child");
thdChild.start();
};
new Thread(rP).start();
}
}
输出结果:


浙公网安备 33010602011771号