0

ThreadLocal is often used as a class static filed like

public static ThreadLocal<String> userSessionName = new ThreadLocal();

That is to say, userSessionName is a GC root,and it would never be collected until the class is unloaded even if the thread id dead. So, when will the Entry in ThreadLocalMap return null after calling get method,in other words,when will the ThreadLocalMap.Entry's key be null ?

rahul rai
  • 2,260
  • 1
  • 7
  • 17
BieFeNg27
  • 1
  • 3
  • The question of when the `ThreadLocal` itself is collected has nothing to do with the question of when its keys or entries get collected. – user207421 Aug 25 '20 at 11:15
  • tks,i want to know the meaning for the usage of weak reference in ThreadLocalMap,when it's key refer a static field and the thread keep alive along with the application lifecycle. – BieFeNg27 Aug 26 '20 at 05:11

1 Answers1

2

A thread's ThreadLocalMap remains reachable as long as the thread is alive. The reference to the map is broken when the thread exits; i.e. before the Thread becomes unreachable.

Entries in the map are reachable as long as the map is reachable. Entries can be removed from the map explicitly by calling ThreadLocal.remove ... but this doesn't happen automatically.

(Actually, this is not entirely correct. Read on.)


The following comments in the Java 11 version of the code partially address your followup question:

ThreadLocalMap is a customized hash map suitable only for maintaining thread local values. No operations are exported outside of the ThreadLocal class. The class is package private to allow declaration of fields in class Thread. To help deal with very large and long-lived usages, the hash table entries use WeakReferences for keys. However, since reference queues are not used, stale entries are guaranteed to be removed only when the table starts running out of space.

The keys that this is referring to are WeakReference<ThreadLocal<?>>.

If the application has a strong (i.e. ordinary) reference to its ThreadLocal object, any corresponding values in any map for any extant thread will be kept. If the application loses its (strong) reference to the ThreadLocal, then corresponding values may be removed. This will only occur when:

  • there is sufficient memory pressure to cause the WeakReference to break, AND
  • the specific ThreadLocalMap is getting a lot of activity.

(The "running out of space" comment don't entirely line up with what the code does. It seems to be a bit more aggressive than that. Look at the code and make up your own mind. Noting that this is implementation dependent behavior.)

But either way, if the application still has a strongly reachable reference to a ThreadLocal object, corresponding values in maps won't be nulled ... unless the application does it explicitly.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • when thread owned by thread pool,the reference to the map will never be broken down.Entry's key will always refer the static ThreadLocal field userSessionName ,so,what's the meaning for the usage of weak reference in ThreadLocalMap. – BieFeNg27 Aug 26 '20 at 05:08