Interesting question. As Tom Hawtin - tackline explained, you are basically creating to instances of ThreadLocal<String>()
. Now let's have a look at how ThreadLocal
actually stores the values (simplified):
public void set(T value) {
ThreadLocalMap map = getMap(Thread.currentThread());
map.set(this, value);
}
It takes some sort of a map that is bound to every thread and sets the value using this
(myself) as a key. This means that if you have two ThreadLocals
(created by different class loaders), they have different this
reference, thus effectively storing different values.
All in all - you cannot e.g. use ThreadLocal
as a workaround to class-loader local singletons and creating thread-local ones.