AQS 源码里,为什么 head 和 tail 是个成员变量而不是静态变量啊
資深大佬 : amiwrong123 0
现在源码也没看完,但这点感觉好难理解啊。
/** * Head of the wait queue, lazily initialized. Except for * initialization, it is modified only via method setHead. Note: * If head exists, its waitStatus is guaranteed not to be * CANCELLED. */ private transient volatile Node head; /** * Tail of the wait queue, lazily initialized. Modified only via * method enq to add new wait node. */ private transient volatile Node tail;
现在知道 CHL 队列 是一个 Node 的双向链表,而好多操作里面都需要获得 head/tail 成员(即每个 Node 都是知道,队列的 head 和 tail 是哪个 Node ),那这岂不是要时刻保持 双向链表里每个 Node 的 head 和 tail 都是正确的。
那把 head 和 tail 设置为静态变量,岂不是方便了很多。反正同时只有一个 Node 是 head 嘛
大佬有話說 (4)