回答

收藏

AQS唤醒后继线程 为什么是从后向前遍历

技术问答 技术问答 734 人阅读 | 0 人回复 | 2023-09-11

    * C2 V; z% T# P: @
  •     private void unparkSuccessor(Node node) {5 z: q$ I6 s$ D0 R# C8 U
  •         /*, T8 N, Y/ A) G0 L
  •          * If status is negative (i.e., possibly needing signal) try
    6 O8 d: S; }5 x$ Q
  •          * to clear in anticipation of signalling.  It is OK if this+ S$ Z' p% r7 S+ W6 p, C2 Q+ a  C
  •          * fails or if status is changed by waiting thread.
    . B, L3 @, R) \; j+ p
  •          */
    / U* j5 [4 E6 C" W
  •         int ws = node.waitStatus;4 `- m8 \, C, U* A
  •         if (ws < 0)
    % [6 f# |" P. }; l  v/ d* ^! _6 ~, c
  •             compareAndSetWaitStatus(node, ws, 0);, D$ P2 _- m$ q3 V
  • ' Z: i' G7 D: {: `
  •         /*
      I* C9 e* B- p; w
  •          * Thread to unpark is held in successor, which is normally2 C/ n. P' x$ L9 U* T% h. ~
  •          * just the next node.  But if cancelled or apparently null,7 I0 ?% t3 B* v# h
  •          * traverse backwards from tail to find the actual
    * t: E0 k- g2 c$ z# U; F
  •          * non-cancelled successor.
    + k6 r8 z8 H9 l* K
  •          */  G. M. t7 y+ m/ @6 x* P
  •         Node s = node.next;  `- y7 c" C/ n9 f1 d
  •         if (s == null || s.waitStatus > 0) {
      |  r! V7 Q# {" A4 c( ~' S
  •             s = null;
    " f$ ]5 `4 Y4 s3 t% G5 X5 p$ A
  •             for (Node t = tail; t != null && t != node; t = t.prev)+ l4 H  H. ]6 C2 v2 K1 q% k% r/ ^
  •                 if (t.waitStatus <= 0)6 W% P7 A" K) }! g0 C; b
  •                     s = t;/ w9 q8 \+ S9 g
  •         }
    2 ~6 [, M5 d# _9 x3 B9 C
  •         if (s != null)
    8 R5 a  U' ]4 y8 t  _9 i
  •             LockSupport.unpark(s.thread);    }4 c4 b$ \1 ?2 w/ O1 w- s) l
如果没有合法的数据,从后向前遍历,为什么这么做呢?从前向后不行吗?
& Z, }5 ]# Y* m9 |* Y
! \  U5 B# j: `" l           lgd1152已获得悬赏 5 金钱最佳答案/ |, y) E( D6 g
6 t, P2 z& W9 z/ X8 M8 }. R
              因为从前往后会遇到空指针               . g7 }9 \0 B$ b: `" y# z; z; G6 u
            / j6 P( N( u/ p2 o* L- l
  • lgd1152        
    8 j* K' V1 L& M" T2 E$ K
  • 发表于2022-01-15        
    4 _- T& L: a2 G% P
  • 详细答案 >    4 {8 \1 J; ?1 @

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则