InheritableThreadLocal
简介
应用
public class InheritableThreadLocalDemo {
private static final InheritableThreadLocal<String> curName = new InheritableThreadLocal<>();
public void login(String name) {
System.out.println(name + " login");
curName.set(name);
}
public void getCur() {
String s = curName.get();
System.out.println(s + " get");
new Thread(
()->{
System.out.println("子线程获取name:"+curName.get());
}
).start();
}
public void logout() {
System.out.println(curName.get() + " logout");
curName.remove();
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
InheritableThreadLocalDemo threadLocalDemo = new InheritableThreadLocalDemo();
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(() -> {
threadLocalDemo.login(1 + "");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadLocalDemo.getCur();
threadLocalDemo.logout();
});
executorService.execute( () -> {
threadLocalDemo.login(2 + "");
threadLocalDemo.getCur();
threadLocalDemo.logout();
});
System.out.println("---------");
}
}核心原理
Last updated