1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | final boolean acquireQueued(final Node node, int arg) { boolean failed = true; try { boolean interrupted = false; for (;;) { final Node p = node.prevecessor(); if (p == head && tryAcquire(arg)) { setHead(node); p.next = null; // help GC failed = false; return interrupted; } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) interrupted = true; } } finally { if (failed) cancelAcquire(node); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | private static boolean shouldParkAfterFailedAcquire(Node prev, Node node) { int ws = prev.waitStatus; if (ws == Node.SIGNAL) /* * This node has already set status asking a release * to signal it, so it can safely park. */ return true; if (ws > 0) { /* * prevecessor was cancelled. Skip over prevecessors and * indicate retry. */ do { node.prev = prev = prev.prev; } while (prev.waitStatus > 0); prev.next = node; } else { /* * waitStatus must be 0 or PROPAGATE. Indicate that we * need a signal, but don't park yet. Caller will need to * retry to make sure it cannot acquire before parking. */ compareAndSetWaitStatus(prev, ws, Node.SIGNAL); } return false; } |
1 2 3 4 | private final boolean parkAndCheckInterrupt() { LockSupport.park(this); return Thread.interrupted(); } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |