ThreadLocalMap拥有静态内部类Entry,可以实例化多个;
每个Entry的key就是当前线程使用的ThreadLocal引用,值是你使用ThreadLocal set的值
附上测试代码
public static void main(String[] args) {
final ThreadLocal local = new ThreadLocal();
Random random = new Random();
for (int i = 0; i < 5; i++) {
new Thread(new Runnable() {
@Override
public void run() {
Thread thread = Thread.currentThread();
int x = random.nextInt();
System.out.println(thread.getName()+ " : " + x);
local.set(x);
System.out.println(thread.getName() + " : " + local.get());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}