Understand ThreadLocal
Every Thread has a ThreadLocal.ThreadLocalMap field
With TheadLocal, every thread can have its own copy of the value. How come?
It’s due to the ThreadLocalMap field in the Thread class. The thread will store the (key, value) in that map. So that it can get back the value later by thread. The key is the ThreadLocal
Btw, ThreadLocalMap is an inner static class of ThreadLocal. It’s a hash table backed by an array. It uses open address method to handle hash collision. The map is not initialized until someone tries to access the stored thread-local value.