回答

收藏

CAS唤醒后继线程 为什么是从后向前遍历5金钱.anslist{background:url(sourc

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


    / _0 q: c3 I" T7 g4 ^0 {5 l2 n% w
  •     private void unparkSuccessor(Node node) {
    3 O0 Z6 {) d6 s2 a5 ~
  •         /*) v" V. _0 X' V$ H/ p3 X2 t
  •          * If status is negative (i.e., possibly needing signal) try
    : u; _3 ~- a/ f
  •          * to clear in anticipation of signalling.  It is OK if this
    / o. e. n! s7 w' P
  •          * fails or if status is changed by waiting thread.
    " R# U# z) ?9 U! C5 n7 I
  •          */1 B( D7 E3 |, j  o1 K
  •         int ws = node.waitStatus;
    # E$ Z/ s& [: @/ M; n; M
  •         if (ws < 0)
    + Z- `( y$ e5 Y+ R
  •             compareAndSetWaitStatus(node, ws, 0);, a$ t4 o3 r0 ^; h% d0 R

  • ! Y% Y* x, w1 v; t6 a' O" @
  •         /*
    8 ?5 a7 L' S2 ]! c" k) }; Z& L6 J
  •          * Thread to unpark is held in successor, which is normally8 R2 K% q( H" X) ~9 O$ ^; ~8 c. d
  •          * just the next node.  But if cancelled or apparently null,
    3 c" w- U$ ]* }4 `# O6 y( o
  •          * traverse backwards from tail to find the actual! e1 B  @$ I# T
  •          * non-cancelled successor.
    2 O$ C% z1 u4 m2 K) p
  •          */5 @7 D( P0 g5 s7 K5 c4 l8 S
  •         Node s = node.next;: ]8 Z4 b+ Y1 c) \$ B2 R* j
  •         if (s == null || s.waitStatus > 0) {* c0 |8 C% k( f% {- {) `( a6 ^
  •             s = null;
    6 r# ?0 i+ Y2 n; e! O4 e' \
  •             for (Node t = tail; t != null && t != node; t = t.prev)- M, W! r/ t6 |' y1 X* |; t- B
  •                 if (t.waitStatus <= 0)- f( b# n/ G. ~( ~( s% |
  •                     s = t;" ^5 r# j! l3 h$ ^  R
  •         }
    7 N' J5 q, D: h- J
  •         if (s != null)
      T7 q2 `6 O' Q. ~
  •             LockSupport.unpark(s.thread);    }
    : e$ c4 [1 L* [
如果没有合法的数据,从后向前遍历,为什么这么做呢?从前向后不行吗?- E+ o# E7 Y. Q" ~4 ^
我知道答案 回答被采纳将会获得5 金钱 已有0人回答
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则