AQS的hasQueuedPredecessors()方法

public final boolean hasQueuedPredecessors() {
    // The correctness of this depends on head being initialized
    // before tail and on head.next being accurate if the current
    // thread is first in queue.
    Node t = tail; // Read fields in reverse initialization order
    Node h = head;
    Node s;
    return h != t &&
        ((s = h.next) == null || s.thread != Thread.currentThread());
}
返回true:

    1 h != t 爲true,(s = h.next) == null 爲true

       1.1 t爲null,h不爲null, AQS同步隊列初始化時,設置head成功,
       還未執行後續的 tail = head;

          

         

      1.2  t不爲null,h不爲null, AQS同步隊列初始化時,設置head、tail成功,還未將head節點
      的next指向待加入線程節點時,即未執行 t.next = node

2  h != t 爲true,(s = h.next) == null 爲false, s.thread != Thread.currentThread()爲true;
說明AQS同步隊列中已存在線程節點,並且head節點的下一個節點不是此時要判斷的線程節點


返回false:

    1 h != t 爲false

       1.1 head與tail都爲null

      

1.2 head==tail,但都不爲null

      

 2、 h != t 爲true, (s = h.next) == null 爲false,s.thread != Thread.currentThread() 也爲false;
第一個判斷說明head節點不等於tail節點;第二個判斷,head節點的下一個節點不爲空,說明AQS同步隊列是存在線程節點的;
第三個判斷說明head節點的下一個的線程節點中, 存儲的線程對象是當前要判斷的線程

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章