Conceptually, you can think of a ThreadLocal
In Thread class, there are two private variables:
ThreadLocal.Values localValues;
ThreadLocal.Values inheritableValues;
Here is how the real per-thread values are stored. (ThreadLocal.set()):
public void set(T value) {
Thread currentThread = Thread.currentThread();
Values values = values(currentThread);
if (values == null) {
values = initializeValues(currentThread);
}
values.put(this, value);
}
Values values(Thread current) {
return current.localValues;
}
Values initializeValues(Thread current) {
return current.localValues = new Values();
}
From the above code, we may see that ThreadLocal set values into Thread object's
localValues variable.When the thread terminates, the thread-specific values can be GCed.
For details please see: crazybob.org: Hard Core Java: ThreadLocal
No comments:
Post a Comment