package cn.bzxy.thread;
public class ThreadLocalTest {
private static final ThreadLocal<User> userLocal=new ThreadLocal<ThreadLocalTest.User>();
public static void main(String[] args) {
User user=getUser();
new Thread(new Runnable() {
@Override
public void run() {
User user=getUser();
}
}).start();
User user2=getUser();
}
private static User getUser(){
User user=userLocal.get();
if(user== null){
user=new User();
userLocal.set(user);
}
return user;
}
public static class User{
public User(){
System.out.println("user constructor "+Thread.currentThread().getName());
}
}
}