Interest Ask. As Tom Hawtin - tackline explained, you basically create ThreadLocal<String>() instances. Now let's see how ThreadLocal actually stores values (simplified):
public void set(T value) { ThreadLocalMap map = getMap(Thread.currentThread()); map.set(this, value); }
It takes some kind of map attached to each thread, and sets the value using this (I) as the key. This means that if you have two ThreadLocals (created by different class loaders), they have a different this link, thereby effectively storing different values.
In general - you cannot, for example. use ThreadLocal as a workaround for local loaders of the loader class and create local threads.
source share