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

📄 abstractqueuedsynchronizer.java

📁 JAVA的一些源码 JAVA2 STANDARD EDITION DEVELOPMENT KIT 5.0
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    /**     * 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 <tt>true</tt> if this object is now in a fully released state,      * so that any waiting threads may attempt to acquire; and <tt>false</tt>     * 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(int arg) {        throw new UnsupportedOperationException();    }    /**     * Returns 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 true if synchronization is held exclusively;     * else false     * @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(int 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(int 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 true if acquired; false if timed out     * @throws InterruptedException if the current thread is interrupted     */   public final boolean tryAcquireNanos(int 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(int 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(int 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(int 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 true if acquired; false if timed out     * @throws InterruptedException if the current thread is interrupted     */   public final boolean tryAcquireSharedNanos(int 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(int 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 <tt>true</tt> return does not guarantee that any     * other thread will ever acquire.     *     * <p> In this implementation, this operation returns in     * constant time.     *     * @return true if there may be other threads waiting to acquire     * the lock.     */    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 true if there has ever been contention     */    public final boolean hasContended() {        return head != null;    }    /**     * Returns the first (longest-waiting) thread in the queue, or     * <tt>null</tt> if no threads are currently queued.     *     * <p> In this implementation, this operation normally returns in     * constant time, but may iterate upon contention if other threads are     * concurrently modifying the queue.     *     * @return the first (longest-waiting) thread in the queue, or     * <tt>null</tt> if no threads are currently queued.     */    public final Thread getFirstQueuedThread() {        // handle only fast path, else relay        return (head == tail)? null : fullGetFirstQueuedThread();    }    /**     * Version of getFirstQueuedThread called when fastpath fails     */    private Thread fullGetFirstQueuedThread() {            Node h = head;            if (h == null)                    // No queue                return null;            /*             * The first node is normally h.next. Try to get its             * thread field, ensuring consistent reads: If thread             * field is nulled out or s.prev is no longer head, then             * some other thread(s) concurrently performed setHead in         * between some of our reads.             */            Node s = h.next;            if (s != null) {                Thread st = s.thread;                Node sp = s.prev;                if (st != null && sp == head)                    return st;            }            /*         * Head's next field might not have been set yet, or may have         * been unset after setHead. So we must check to see if tail         * is actually first node. If not, we continue on, safely         * traversing from tail back to head to find first,         * guaranteeing termination.             */        Node t = tail;        Thread firstThread = null;        while (t != null && t != head) {                Thread tt = t.thread;            if (tt != null)                firstThread = tt;            t = t.prev;        }        return firstThread;    }    /**     * Returns true if the given thread is currently queued.      *     * <p> This implementation traverses the queue to determine     * presence of the given thread.     *     * @param thread the thread     * @return true if the given thread in on the queue     * @throws NullPointerException if thread null     */    public final boolean isQueued(Thread thread) {        if (thread == null)            throw new NullPointerException();        for (Node p = tail; p != null; p = p.prev)            if (p.thread == thread)                return true;        return false;    }    // Instrumentation and monitoring methods    /**     * Returns an estimate of the number of threads waiting to     * acquire.  The value is only an estimate because the number of     * threads may change dynamically while this method traverses     * internal data structures.  This method is designed for use in     * monitoring system state, not for synchronization     * control.     *     * @return the estimated number of threads waiting for this lock     */    public final int getQueueLength() {        int n = 0;        for (Node p = tail; p != null; p = p.prev) {            if (p.thread != null)                ++n;        }        return n;    }    /**     * Returns a collection containing threads that may be waiting to     * acquire.  Because the actual set of threads may change     * dynamically while constructing this result, the returned     * collection is only a best-effort estimate.  The elements of the     * returned collection are in no particular order.  This method is     * designed to facilitate construction of subclasses that provide     * more extensive monitoring facilities.

⌨️ 快捷键说明

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