publicfinalvoidacquireInterruptibly(int arg) throws InterruptedException { if (Thread.interrupted()) thrownewInterruptedException(); if (!tryAcquire(arg)) doAcquireInterruptibly(arg); } //加入排队, privatevoiddoAcquireInterruptibly(int arg) throws InterruptedException { finalNodenode= addWaiter(Node.EXCLUSIVE); try { for (;;) { finalNodep= node.predecessor(); if (p == head && tryAcquire(arg)) { setHead(node); p.next = null; // help GC return; } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) thrownewInterruptedException(); } } catch (Throwable t) { cancelAcquire(node); throw t; } } //在获取锁失败是否进入等待 privatestaticbooleanshouldParkAfterFailedAcquire(Node pred, Node node) { intws= pred.waitStatus; if (ws == Node.SIGNAL) /* * This node has already set status asking a release * to signal it, so it can safely park. */ returntrue; if (ws > 0) { /* * Predecessor was cancelled. Skip over predecessors and * indicate retry. */ do { node.prev = pred = pred.prev; } while (pred.waitStatus > 0); pred.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. */ pred.compareAndSetWaitStatus(ws, Node.SIGNAL); } returnfalse; }