qq_33560566
Big_Double
2019-03-20 02:59

Java AQS如何实现非公平锁

5
  • java

最近看了AQS的源码 但是看完之后有很多问题

final boolean acquireQueued(final Node node, int arg) {
    boolean failed = true;
    try {
        boolean interrupted = false;
        for (;;) {
            final Node p = node.predecessor();
            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);
    }
}

private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
    int ws = pred.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) {
        /*
         * 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.
         */
        compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
    }
    return false;
}

这是自旋请求资源的方法,但是shouldParkAfterFailedAcquire()方法中,可以得知,在队列中等待的线程,基本都会被park,然后来看release()

public final boolean release(int arg) {
    if (tryRelease(arg)) {
        Node h = head;
        if (h != null && h.waitStatus != 0)
            unparkSuccessor(h);
        return true;
    }
    return false;
}

unparkSuccessor将会唤醒后继节点,但是只唤醒了一个,所以我产生了一个疑问,如果只唤醒了一个线程,那么用aqs实现非公平锁,在tryRelease中直接抢锁,但是抢锁失败后,加入队列中,仍然需要按照顺序被唤醒,还是不是完完全全的非公平,但是总觉得是不是有问题,请大神指点我一下

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐