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

📄 abstractqueuedsynchronizer.java

📁 JAVA的一些源码 JAVA2 STANDARD EDITION DEVELOPMENT KIT 5.0
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * @return the collection of threads     */    public final Collection<Thread> getQueuedThreads() {        ArrayList<Thread> list = new ArrayList<Thread>();        for (Node p = tail; p != null; p = p.prev) {            Thread t = p.thread;            if (t != null)                list.add(t);        }        return list;    }    /**     * Returns a collection containing threads that may be waiting to     * acquire in exclusive mode. This has the same properties     * as {@link #getQueuedThreads} except that it only returns     * those threads waiting due to an exclusive acquire.     * @return the collection of threads     */    public final Collection<Thread> getExclusiveQueuedThreads() {        ArrayList<Thread> list = new ArrayList<Thread>();        for (Node p = tail; p != null; p = p.prev) {            if (!p.isShared()) {                Thread t = p.thread;                if (t != null)                    list.add(t);            }        }        return list;    }    /**     * Returns a collection containing threads that may be waiting to     * acquire in shared mode. This has the same properties     * as {@link #getQueuedThreads} except that it only returns     * those threads waiting due to a shared acquire.     * @return the collection of threads     */    public final Collection<Thread> getSharedQueuedThreads() {        ArrayList<Thread> list = new ArrayList<Thread>();        for (Node p = tail; p != null; p = p.prev) {            if (p.isShared()) {                Thread t = p.thread;                if (t != null)                    list.add(t);            }        }        return list;    }    /**     * Returns a string identifying this synchronizer, as well as its     * state.  The state, in brackets, includes the String &quot;State     * =&quot; followed by the current value of {@link #getState}, and     * either &quot;nonempty&quot; or &quot;empty&quot; depending on     * whether the queue is empty.     *     * @return a string identifying this synchronizer, as well as its state.     */    public String toString() {        int s = getState();        String q  = hasQueuedThreads()? "non" : "";        return super.toString() +             "[State = " + s + ", " + q + "empty queue]";    }    // Internal support methods for Conditions    /**     * Returns true if a node, always one that was initially placed on     * a condition queue, is now waiting to reacquire on sync queue.     * @param node the node     * @return true if is reacquiring     */    final boolean isOnSyncQueue(Node node) {        if (node.waitStatus == Node.CONDITION || node.prev == null)            return false;        if (node.next != null) // If has successor, it must be on queue            return true;        /*         * node.prev can be non-null, but not yet on queue because         * the CAS to place it on queue can fail. So we have to         * traverse from tail to make sure it actually made it.  It         * will always be near the tail in calls to this method, and         * unless the CAS failed (which is unlikely), it will be         * there, so we hardly ever traverse much.         */        return findNodeFromTail(node);    }     /**     * Returns true if node is on sync queue by searching backwards from tail.     * Called only when needed by isOnSyncQueue.     * @return true if present     */    private boolean findNodeFromTail(Node node) {        Node t = tail;         for (;;) {            if (t == node)                return true;            if (t == null)                return false;            t = t.prev;        }    }    /**     * Transfers a node from a condition queue onto sync queue.      * Returns true if successful.     * @param node the node     * @return true if successfully transferred (else the node was     * cancelled before signal).     */    final boolean transferForSignal(Node node) {        /*         * If cannot change waitStatus, the node has been cancelled.         */        if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))            return false;        /*         * Splice onto queue and try to set waitStatus of predecessor to         * indicate that thread is (probably) waiting. If cancelled or         * attempt to set waitStatus fails, wake up to resync (in which         * case the waitStatus can be transiently and harmlessly wrong).         */        Node p = enq(node);        int c = p.waitStatus;        if (c > 0 || !compareAndSetWaitStatus(p, c, Node.SIGNAL))             LockSupport.unpark(node.thread);        return true;    }    /**     * Transfers node, if necessary, to sync queue after a cancelled     * wait. Returns true if thread was cancelled before being     * signalled.     * @param current the waiting thread     * @param node its node     * @return true if cancelled before the node was signalled.     */    final boolean transferAfterCancelledWait(Node node) {        if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) {            enq(node);            return true;        }        /*         * If we lost out to a signal(), then we can't proceed         * until it finishes its enq().  Cancelling during an         * incomplete transfer is both rare and transient, so just         * spin.         */        while (!isOnSyncQueue(node))             Thread.yield();        return false;    }    /**     * Invoke release with current state value; return saved state.     * Cancel node and throw exception on failure.     * @param node the condition node for this wait     * @return previous sync state     */    final int fullyRelease(Node node) {        try {            int savedState = getState();            if (release(savedState))                return savedState;        } catch(RuntimeException ex) {            node.waitStatus = Node.CANCELLED;            throw ex;        }        // reach here if release fails        node.waitStatus = Node.CANCELLED;        throw new IllegalMonitorStateException();    }    // Instrumentation methods for conditions    /**     * Queries whether the given ConditionObject      * uses this synchronizer as its lock.     * @param condition the condition     * @return <tt>true</tt> if owned     * @throws NullPointerException if condition null     */    public final boolean owns(ConditionObject condition) {        if (condition == null)            throw new NullPointerException();        return condition.isOwnedBy(this);    }    /**     * Queries whether any threads are waiting on the given condition     * associated with this synchronizer. Note that because timeouts     * and interrupts may occur at any time, a <tt>true</tt> return     * does not guarantee that a future <tt>signal</tt> will awaken     * any threads.  This method is designed primarily for use in     * monitoring of the system state.     * @param condition the condition     * @return <tt>true</tt> if there are any waiting threads.     * @throws IllegalMonitorStateException if exclusive synchronization      * is not held     * @throws IllegalArgumentException if the given condition is     * not associated with this synchronizer     * @throws NullPointerException if condition null     */     public final boolean hasWaiters(ConditionObject condition) {        if (!owns(condition))            throw new IllegalArgumentException("Not owner");        return condition.hasWaiters();    }    /**     * Returns an estimate of the number of threads waiting on the     * given condition associated with this synchronizer. Note that     * because timeouts and interrupts may occur at any time, the     * estimate serves only as an upper bound on the actual number of     * waiters.  This method is designed for use in monitoring of the     * system state, not for synchronization control.     * @param condition the condition     * @return the estimated number of waiting threads.     * @throws IllegalMonitorStateException if exclusive synchronization      * is not held     * @throws IllegalArgumentException if the given condition is     * not associated with this synchronizer     * @throws NullPointerException if condition null     */     public final int getWaitQueueLength(ConditionObject condition) {        if (!owns(condition))            throw new IllegalArgumentException("Not owner");        return condition.getWaitQueueLength();    }    /**     * Returns a collection containing those threads that may be     * waiting on the given condition associated with this     * synchronizer.  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.       * @param condition the condition     * @return the collection of threads     * @throws IllegalMonitorStateException if exclusive synchronization      * is not held     * @throws IllegalArgumentException if the given condition is     * not associated with this synchronizer     * @throws NullPointerException if condition null     */    public final Collection<Thread> getWaitingThreads(ConditionObject condition) {        if (!owns(condition))            throw new IllegalArgumentException("Not owner");        return condition.getWaitingThreads();    }    /**     * Condition implementation for a {@link     * AbstractQueuedSynchronizer} serving as the basis of a {@link     * Lock} implementation.     *     * <p> Method documentation for this class describes mechanics,     * not behavioral specifications from the point of view of Lock     * and Condition users. Exported versions of this class will in     * general need to be accompanied by documentation describing     * condition semantics that rely on those of the associated     * <tt>AbstractQueuedSynchronizer</tt>.     *      * <p> This class is Serializable, but all fields are transient,     * so deserialized conditions have no waiters.     */    public class ConditionObject implements Condition, java.io.Serializable {        private static final long serialVersionUID = 1173984872572414699L;        /** First node of condition queue. */        private transient Node firstWaiter;        /** Last node of condition queue. */        private transient Node lastWaiter;        /**         * Creates a new <tt>ConditionObject</tt> instance.         */        public ConditionObject() { }        // Internal methods        /**         * Add a new waiter to wait queue         * @return its new wait node         */        private Node addConditionWaiter() {            Node t = lastWaiter;            // If lastWaiter is cancelled, clean out.            if (t != null && t.waitStatus != Node.CONDITION) {                unlinkCancelledWaiters();                t = lastWaiter;            }            Node node = new Node(Thread.currentThread(), Node.CONDITION);            if (t == null)                firstWaiter = node;            else                t.nextWaiter = node;            lastWaiter = node;            return node;        }        /**         * Remove and transfer nodes until hit non-cancelled one or         * null. Split out from signal in part to encourage compilers         * to inline the case of no waiters.         * @param first (non-null) the first node on condition queue         */        private void doSignal(Node first) {            do {                if ( (firstWaiter = first.nextWaiter) == null)                     lastWaiter = null;                first.nextWaiter = null;            } while (!transferForSignal(first) &&                     (first = firstWaiter) != null);        }        /**         * Remove and transfer all nodes.         * @param first (non-null) the first node on condition queue         */        private void doSignalAll(Node first) {            lastWaiter = firstWaiter  = null;            do {                Node next = first.nextWaiter;                first.nextWaiter = null;                transferForSignal(first);                first = next;            } while (first != null);        }        // public methods        /**         * Moves the longest-waiting thread, i

⌨️ 快捷键说明

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