ThreadLocalMap类中的弱引用WeakReference
public abstract class Reference<T> {
看源代码和注释发现。
引用Reference作为抽象类,包含一个私有的T referent --------中文翻译为"指示物"
A Reference instance is in one of four possible internal states:
Active:Subject to special treatment by the garbage collector
Pending:An element of the pending-Reference list, waiting to be enqueued by the Reference-handler thread. Unregistered instances are never in this state.
Enqueued:An element of the queue with which the instance was registered when it was created. When an instance is removed from its ReferenceQueue, it is made Inactive.
Inactive:Nothing more to do. Once an instance becomes Inactive its state will never change again.
可以看到关键字是4种状态Active激活,Pending等待,Enqueued排队,Inactive失效
instance---->registered--->in pending-Reference list(Pending)---->into ReferenceQueue(Enqueued)---->removed from ReferenceQueue(Inactive)
即实例---->注册---->进入等待状态----进入到队列内后排队状态----从队列中移除后失效
/* When active: NULL * pending: this * Enqueued: next reference in queue (or this if last) * Inactive: this */ @SuppressWarnings("rawtypes") Reference next; /* When active: next element in a discovered reference list maintained by GC (or this if last) * pending: next element in the pending list (or null if last) * otherwise: NULL */ transient private Reference<T> discovered; /* used by VM */ /* Object used to synchronize with the garbage collector. The collector * must acquire this lock at the beginning of each collection cycle. It is * therefore critical that any code holding this lock complete as quickly * as possible, allocate no new objects, and avoid calling user code. */ static private class Lock { } private static Lock lock = new Lock(); /* List of References waiting to be enqueued. The collector adds * References to this list, while the Reference-handler thread removes * them. This list is protected by the above lock object. The * list uses the discovered field to link its elements. */ private static Reference<Object> pending = null;