Java多线程系列--“JUC锁”05之 非公平锁(九)
or use in monitoring of the system
714 * state, not for synchronization control.
715 *
716 * @param condition the condition
717 * @return the estimated number of waiting threads
718 * @throws IllegalMonitorStateException if this lock is not held
719 * @throws IllegalArgumentException if the given condition is
720 * not associated with this lock
721 * @throws NullPointerException if the condition is null
722 */
723 public int getWaitQueueLength(Condition condition) {
724 if (condition == null)
725 throw new NullPointerException();
726 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
727 throw new IllegalArgumentException("not owner");
728 return sync.getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject)condition);
729 }
730
731 /**
732 * Returns a collection containing those threads that may be
733 * waiting on the given condition associated with this lock.
734 * Because the actual set of threads may change dynamically while
735 * constructing this result, the returned collection is only a
736 * best-effort estimate. The elements of the returned collection
737 * are in no particular order. This method is designed to
738 * facilitate construction of subclasses that provide more
739 * extensive condition monitoring facilities.
740 *
741 * @param condition the condition
742 * @return the collection of threads
743 * @throws IllegalMonitorStateException if this lock is not held
744 * @throws IllegalArgumentException if the given condition is
745 * not associated with this lock
746 * @throws NullPointerException if the condition is null
747 */
748 protected Collection getWaitingThreads(Condition condition) {
749 if (condition == null)
750 throw new NullPointerException();
751 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
752 throw new IllegalArgumentException("not owner");
753 return sync.getWaitingThreads((AbstractQueuedSynchronizer.ConditionObject)condition);
754 }
755
756 /**
757 * Returns a string identifying this lock, as well as its lock state.
758 * The state, in brackets, includes either the String {@code "Unlocked"}
759 * or the String {@code "Locked by"} followed by the
760 * {@linkplain Thread#getName name} of the owning thread.
761 *
762 * @return a string identifying this lock, as well as its lock state
763 */
764 public String toString() {
765 Thread o = sync.getOwner();
766 return super.toString() + ((o == null)
767 "[Unlocked]" :
768 "[Locked by thread " + o.getName() + "]");
769 }
770 }