Java多线程系列--“JUC锁”05之 非公平锁(八)

2014-11-24 03:00:33 · 作者: · 浏览: 9
es whether the given thread is waiting to acquire this
642 * lock. Note that because cancellations may occur at any time, a
643 * {@code true} return does not guarantee that this thread
644 * will ever acquire this lock. This method is designed primarily for use
645 * in monitoring of the system state.
646 *
647 * @param thread the thread
648 * @return {@code true} if the given thread is queued waiting for this lock
649 * @throws NullPointerException if the thread is null
650 */
651 public final boolean hasQueuedThread(Thread thread) {
652 return sync.isQueued(thread);
653 }
654
655
656 /**
657 * Returns an estimate of the number of threads waiting to
658 * acquire this lock. The value is only an estimate because the number of
659 * threads may change dynamically while this method traverses
660 * internal data structures. This method is designed for use in
661 * monitoring of the system state, not for synchronization
662 * control.
663 *
664 * @return the estimated number of threads waiting for this lock
665 */
666 public final int getQueueLength() {
667 return sync.getQueueLength();
668 }
669
670 /**
671 * Returns a collection containing threads that may be waiting to
672 * acquire this lock. Because the actual set of threads may change
673 * dynamically while constructing this result, the returned
674 * collection is only a best-effort estimate. The elements of the
675 * returned collection are in no particular order. This method is
676 * designed to facilitate construction of subclasses that provide
677 * more extensive monitoring facilities.
678 *
679 * @return the collection of threads
680 */
681 protected Collection getQueuedThreads() {
682 return sync.getQueuedThreads();
683 }
684
685 /**
686 * Queries whether any threads are waiting on the given condition
687 * associated with this lock. Note that because timeouts and
688 * interrupts may occur at any time, a {@code true} return does
689 * not guarantee that a future {@code signal} will awaken any
690 * threads. This method is designed primarily for use in
691 * monitoring of the system state.
692 *
693 * @param condition the condition
694 * @return {@code true} if there are any waiting threads
695 * @throws IllegalMonitorStateException if this lock is not held
696 * @throws IllegalArgumentException if the given condition is
697 * not associated with this lock
698 * @throws NullPointerException if the condition is null
699 */
700 public boolean hasWaiters(Condition condition) {
701 if (condition == null)
702 throw new NullPointerException();
703 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
704 throw new IllegalArgumentException("not owner");
705 return sync.hasWaiters((AbstractQueuedSynchronizer.ConditionObject)condition);
706 }
707
708 /**
709 * Returns an estimate of the number of threads waiting on the
710 * given condition associated with this lock. Note that because
711 * timeouts and interrupts may occur at any time, the estimate
712 * serves only as an upper bound on the actual number of waiters.
713 * This method is designed f