⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractqueuedsynchronizer.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	    // If "active" predecessor found...	    if (pred != head		&& (pred.waitStatus == Node.SIGNAL		    || compareAndSetWaitStatus(pred, 0, Node.SIGNAL))		&& pred.thread != null) {		// If successor is active, set predecessor's next link		Node next = node.next;		if (next != null && next.waitStatus <= 0)		    compareAndSetNext(pred, predNext, next);	    } else {		unparkSuccessor(node);	    }	    node.next = node; // help GC	}    }    /**     * Checks and updates status for a node that failed to acquire.     * Returns true if thread should block. This is the main signal     * control in all acquire loops.  Requires that pred == node.prev     *     * @param pred node's predecessor holding status     * @param node the node     * @return {@code true} if thread should block     */    private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {        int s = pred.waitStatus;        if (s < 0)            /*             * This node has already set status asking a release             * to signal it, so it can safely park             */            return true;        if (s > 0) {            /*             * Predecessor was cancelled. Skip over predecessors and             * indicate retry.             */	    do {		node.prev = pred = pred.prev;	    } while (pred.waitStatus > 0);	    pred.next = node;	}        else            /*             * 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, 0, Node.SIGNAL);        return false;    }    /**     * Convenience method to interrupt current thread.     */    private static void selfInterrupt() {        Thread.currentThread().interrupt();    }    /**     * Convenience method to park and then check if interrupted     *     * @return {@code true} if interrupted     */    private final boolean parkAndCheckInterrupt() {        LockSupport.park(this);        return Thread.interrupted();    }    /*     * Various flavors of acquire, varying in exclusive/shared and     * control modes.  Each is mostly the same, but annoyingly     * different.  Only a little bit of factoring is possible due to     * interactions of exception mechanics (including ensuring that we     * cancel if tryAcquire throws exception) and other control, at     * least not without hurting performance too much.     */    /**     * Acquires in exclusive uninterruptible mode for thread already in     * queue. Used by condition wait methods as well as acquire.     *     * @param node the node     * @param arg the acquire argument     * @return {@code true} if interrupted while waiting     */    final boolean acquireQueued(final Node node, int arg) {        try {            boolean interrupted = false;            for (;;) {                final Node p = node.predecessor();                if (p == head && tryAcquire(arg)) {                    setHead(node);                    p.next = null; // help GC                    return interrupted;                }                if (shouldParkAfterFailedAcquire(p, node) &&                    parkAndCheckInterrupt())                    interrupted = true;            }        } catch (RuntimeException ex) {            cancelAcquire(node);            throw ex;        }    }    /**     * Acquires in exclusive interruptible mode.     * @param arg the acquire argument     */    private void doAcquireInterruptibly(int arg)        throws InterruptedException {        final Node node = addWaiter(Node.EXCLUSIVE);        try {            for (;;) {                final Node p = node.predecessor();                if (p == head && tryAcquire(arg)) {                    setHead(node);                    p.next = null; // help GC                    return;                }                if (shouldParkAfterFailedAcquire(p, node) &&                    parkAndCheckInterrupt())                    break;            }        } catch (RuntimeException ex) {            cancelAcquire(node);            throw ex;        }        // Arrive here only if interrupted        cancelAcquire(node);        throw new InterruptedException();    }    /**     * Acquires in exclusive timed mode.     *     * @param arg the acquire argument     * @param nanosTimeout max wait time     * @return {@code true} if acquired     */    private boolean doAcquireNanos(int arg, long nanosTimeout)        throws InterruptedException {        long lastTime = System.nanoTime();        final Node node = addWaiter(Node.EXCLUSIVE);        try {            for (;;) {                final Node p = node.predecessor();                if (p == head && tryAcquire(arg)) {                    setHead(node);                    p.next = null; // help GC                    return true;                }                if (nanosTimeout <= 0) {                    cancelAcquire(node);                    return false;                }                if (nanosTimeout > spinForTimeoutThreshold &&                    shouldParkAfterFailedAcquire(p, node))                    LockSupport.parkNanos(this, nanosTimeout);                long now = System.nanoTime();                nanosTimeout -= now - lastTime;                lastTime = now;                if (Thread.interrupted())                    break;            }        } catch (RuntimeException ex) {            cancelAcquire(node);            throw ex;        }        // Arrive here only if interrupted        cancelAcquire(node);        throw new InterruptedException();    }    /**     * Acquires in shared uninterruptible mode.     * @param arg the acquire argument     */    private void doAcquireShared(int arg) {        final Node node = addWaiter(Node.SHARED);        try {            boolean interrupted = false;            for (;;) {                final Node p = node.predecessor();                if (p == head) {                    int r = tryAcquireShared(arg);                    if (r >= 0) {                        setHeadAndPropagate(node, r);                        p.next = null; // help GC                        if (interrupted)                            selfInterrupt();                        return;                    }                }                if (shouldParkAfterFailedAcquire(p, node) &&                    parkAndCheckInterrupt())                    interrupted = true;            }        } catch (RuntimeException ex) {            cancelAcquire(node);            throw ex;        }    }    /**     * Acquires in shared interruptible mode.     * @param arg the acquire argument     */    private void doAcquireSharedInterruptibly(int arg)        throws InterruptedException {        final Node node = addWaiter(Node.SHARED);        try {            for (;;) {                final Node p = node.predecessor();                if (p == head) {                    int r = tryAcquireShared(arg);                    if (r >= 0) {                        setHeadAndPropagate(node, r);                        p.next = null; // help GC                        return;                    }                }                if (shouldParkAfterFailedAcquire(p, node) &&                    parkAndCheckInterrupt())                    break;            }        } catch (RuntimeException ex) {            cancelAcquire(node);            throw ex;        }        // Arrive here only if interrupted        cancelAcquire(node);        throw new InterruptedException();    }    /**     * Acquires in shared timed mode.     *     * @param arg the acquire argument     * @param nanosTimeout max wait time     * @return {@code true} if acquired     */    private boolean doAcquireSharedNanos(int arg, long nanosTimeout)        throws InterruptedException {        long lastTime = System.nanoTime();        final Node node = addWaiter(Node.SHARED);        try {            for (;;) {                final Node p = node.predecessor();                if (p == head) {                    int r = tryAcquireShared(arg);                    if (r >= 0) {                        setHeadAndPropagate(node, r);                        p.next = null; // help GC                        return true;                    }                }                if (nanosTimeout <= 0) {                    cancelAcquire(node);                    return false;                }                if (nanosTimeout > spinForTimeoutThreshold &&                    shouldParkAfterFailedAcquire(p, node))                    LockSupport.parkNanos(this, nanosTimeout);                long now = System.nanoTime();                nanosTimeout -= now - lastTime;                lastTime = now;                if (Thread.interrupted())                    break;            }        } catch (RuntimeException ex) {            cancelAcquire(node);            throw ex;        }        // Arrive here only if interrupted        cancelAcquire(node);        throw new InterruptedException();    }    // Main exported methods    /**     * Attempts to acquire in exclusive mode. This method should query     * if the state of the object permits it to be acquired in the     * exclusive mode, and if so to acquire it.     *     * <p>This method is always invoked by the thread performing     * acquire.  If this method reports failure, the acquire method     * may queue the thread, if it is not already queued, until it is     * signalled by a release from some other thread. This can be used     * to implement method {@link Lock#tryLock()}.     *     * <p>The default     * implementation throws {@link UnsupportedOperationException}.     *     * @param arg the acquire argument. This value is always the one     *        passed to an acquire method, or is the value saved on entry     *        to a condition wait.  The value is otherwise uninterpreted     *        and can represent anything you like.     * @return {@code true} if successful. Upon success, this object has     *         been acquired.     * @throws IllegalMonitorStateException if acquiring would place this     *         synchronizer in an illegal state. This exception must be     *         thrown in a consistent fashion for synchronization to work     *         correctly.     * @throws UnsupportedOperationException if exclusive mode is not supported     */    protected boolean tryAcquire(int arg) {        throw new UnsupportedOperationException();    }    /**     * Attempts to set the state to reflect a release in exclusive     * mode.     *     * <p>This method is always invoked by the thread performing release.     *     * <p>The default implementation throws     * {@link UnsupportedOperationException}.     *     * @param arg the release argument. This value is always the one     *        passed to a release method, or the current state value upon     *        entry to a condition wait.  The value is otherwise     *        uninterpreted and can represent anything you like.     * @return {@code true} if this object is now in a fully released     *         state, so that any waiting threads may attempt to acquire;     *         and {@code false} otherwise.     * @throws IllegalMonitorStateException if releasing would place this     *         synchronizer in an illegal state. This exception must be     *         thrown in a consistent fashion for synchronization to work     *         correctly.     * @throws UnsupportedOperationException if exclusive mode is not supported     */    protected boolean tryRelease(int arg) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -