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

📄 abstractqueuedlongsynchronizer.java

📁 JAVA 所有包
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                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(long 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(long arg) {        throw new UnsupportedOperationException();    }    /**     * Attempts to acquire in shared mode. This method should query if     * the state of the object permits it to be acquired in the shared     * 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.     *     * <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 a negative value on failure; zero if acquisition in shared     *         mode succeeded but no subsequent shared-mode acquire can     *         succeed; and a positive value if acquisition in shared     *         mode succeeded and subsequent shared-mode acquires might     *         also succeed, in which case a subsequent waiting thread     *         must check availability. (Support for three different     *         return values enables this method to be used in contexts     *         where acquires only sometimes act exclusively.)  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 shared mode is not supported     */    protected long tryAcquireShared(long arg) {        throw new UnsupportedOperationException();    }    /**     * Attempts to set the state to reflect a release in shared 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 release of shared mode may permit a     *         waiting acquire (shared or exclusive) to succeed; 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 shared mode is not supported     */    protected boolean tryReleaseShared(long arg) {        throw new UnsupportedOperationException();    }    /**     * Returns {@code true} if synchronization is held exclusively with     * respect to the current (calling) thread.  This method is invoked     * upon each call to a non-waiting {@link ConditionObject} method.     * (Waiting methods instead invoke {@link #release}.)     *     * <p>The default implementation throws {@link     * UnsupportedOperationException}. This method is invoked     * internally only within {@link ConditionObject} methods, so need     * not be defined if conditions are not used.     *     * @return {@code true} if synchronization is held exclusively;     *         {@code false} otherwise     * @throws UnsupportedOperationException if conditions are not supported     */    protected boolean isHeldExclusively() {        throw new UnsupportedOperationException();    }    /**     * Acquires in exclusive mode, ignoring interrupts.  Implemented     * by invoking at least once {@link #tryAcquire},     * returning on success.  Otherwise the thread is queued, possibly     * repeatedly blocking and unblocking, invoking {@link     * #tryAcquire} until success.  This method can be used     * to implement method {@link Lock#lock}.     *     * @param arg the acquire argument.  This value is conveyed to     *        {@link #tryAcquire} but is otherwise uninterpreted and     *        can represent anything you like.     */    public final void acquire(long arg) {        if (!tryAcquire(arg) &&            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))            selfInterrupt();    }    /**     * Acquires in exclusive mode, aborting if interrupted.     * Implemented by first checking interrupt status, then invoking     * at least once {@link #tryAcquire}, returning on     * success.  Otherwise the thread is queued, possibly repeatedly     * blocking and unblocking, invoking {@link #tryAcquire}     * until success or the thread is interrupted.  This method can be     * used to implement method {@link Lock#lockInterruptibly}.     *     * @param arg the acquire argument.  This value is conveyed to     *        {@link #tryAcquire} but is otherwise uninterpreted and     *        can represent anything you like.     * @throws InterruptedException if the current thread is interrupted     */    public final void acquireInterruptibly(long arg) throws InterruptedException {        if (Thread.interrupted())            throw new InterruptedException();        if (!tryAcquire(arg))            doAcquireInterruptibly(arg);    }    /**     * Attempts to acquire in exclusive mode, aborting if interrupted,     * and failing if the given timeout elapses.  Implemented by first     * checking interrupt status, then invoking at least once {@link     * #tryAcquire}, returning on success.  Otherwise, the thread is     * queued, possibly repeatedly blocking and unblocking, invoking     * {@link #tryAcquire} until success or the thread is interrupted     * or the timeout elapses.  This method can be used to implement     * method {@link Lock#tryLock(long, TimeUnit)}.     *     * @param arg the acquire argument.  This value is conveyed to     *        {@link #tryAcquire} but is otherwise uninterpreted and     *        can represent anything you like.     * @param nanosTimeout the maximum number of nanoseconds to wait     * @return {@code true} if acquired; {@code false} if timed out     * @throws InterruptedException if the current thread is interrupted     */    public final boolean tryAcquireNanos(long arg, long nanosTimeout) throws InterruptedException {	if (Thread.interrupted())	    throw new InterruptedException();	return tryAcquire(arg) ||	    doAcquireNanos(arg, nanosTimeout);    }    /**     * Releases in exclusive mode.  Implemented by unblocking one or     * more threads if {@link #tryRelease} returns true.     * This method can be used to implement method {@link Lock#unlock}.     *     * @param arg the release argument.  This value is conveyed to     *        {@link #tryRelease} but is otherwise uninterpreted and     *        can represent anything you like.     * @return the value returned from {@link #tryRelease}     */    public final boolean release(long arg) {        if (tryRelease(arg)) {            Node h = head;            if (h != null && h.waitStatus != 0)                unparkSuccessor(h);            return true;        }        return false;    }    /**     * Acquires in shared mode, ignoring interrupts.  Implemented by     * first invoking at least once {@link #tryAcquireShared},     * returning on success.  Otherwise the thread is queued, possibly     * repeatedly blocking and unblocking, invoking {@link     * #tryAcquireShared} until success.     *     * @param arg the acquire argument.  This value is conveyed to     *        {@link #tryAcquireShared} but is otherwise uninterpreted     *        and can represent anything you like.     */    public final void acquireShared(long arg) {        if (tryAcquireShared(arg) < 0)            doAcquireShared(arg);    }    /**     * Acquires in shared mode, aborting if interrupted.  Implemented     * by first checking interrupt status, then invoking at least once     * {@link #tryAcquireShared}, returning on success.  Otherwise the     * thread is queued, possibly repeatedly blocking and unblocking,     * invoking {@link #tryAcquireShared} until success or the thread     * is interrupted.     * @param arg the acquire argument.     * This value is conveyed to {@link #tryAcquireShared} but is     * otherwise uninterpreted and can represent anything     * you like.     * @throws InterruptedException if the current thread is interrupted     */    public final void acquireSharedInterruptibly(long arg) throws InterruptedException {        if (Thread.interrupted())            throw new InterruptedException();        if (tryAcquireShared(arg) < 0)            doAcquireSharedInterruptibly(arg);    }    /**     * Attempts to acquire in shared mode, aborting if interrupted, and     * failing if the given timeout elapses.  Implemented by first     * checking interrupt status, then invoking at least once {@link     * #tryAcquireShared}, returning on success.  Otherwise, the     * thread is queued, possibly repeatedly blocking and unblocking,     * invoking {@link #tryAcquireShared} until success or the thread     * is interrupted or the timeout elapses.     *     * @param arg the acquire argument.  This value is conveyed to     *        {@link #tryAcquireShared} but is otherwise uninterpreted     *        and can represent anything you like.     * @param nanosTimeout the maximum number of nanoseconds to wait     * @return {@code true} if acquired; {@code false} if timed out     * @throws InterruptedException if the current thread is interrupted     */    public final boolean tryAcquireSharedNanos(long arg, long nanosTimeout) throws InterruptedException {	if (Thread.interrupted())	    throw new InterruptedException();	return tryAcquireShared(arg) >= 0 ||	    doAcquireSharedNanos(arg, nanosTimeout);    }    /**     * Releases in shared mode.  Implemented by unblocking one or more     * threads if {@link #tryReleaseShared} returns true.     *     * @param arg the release argument.  This value is conveyed to     *        {@link #tryReleaseShared} but is otherwise uninterpreted     *        and can represent anything you like.     * @return the value returned from {@link #tryReleaseShared}     */    public final boolean releaseShared(long arg) {        if (tryReleaseShared(arg)) {            Node h = head;            if (h != null && h.waitStatus != 0)                unparkSuccessor(h);            return true;        }        return false;    }    // Queue inspection methods    /**     * Queries whether any threads are waiting to acquire. Note that     * because cancellations due to interrupts and timeouts may occur     * at any time, a {@code true} return does not guarantee that any     * other thread will ever acquire.     *     * <p>In this implementation, this operation returns in     * constant time.     *     * @return {@code true} if there may be other threads waiting to acquire     */    public final boolean hasQueuedThreads() {        return head != tail;    }    /**     * Queries whether any threads have ever contended to acquire this     * synchronizer; that is if an acquire method has ever blocked.     *     * <p>In this implementation, this operation returns in     * constant time.     *     * @return {@code true} if there has ever been contention     */    public final boolean hasContended() {        return head != null;    }    /**     * Returns the first (longest-waiting) thread in the queue, or     * {@code null} if no threads are currently queued.

⌨️ 快捷键说明

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