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

2014-11-24 03:00:33 · 作者: · 浏览: 15
* Queries if this lock is held by the current thread.
542 *
543 *

Analogous to the {@link Thread#holdsLock} method for built-in

544 * monitor locks, this method is typically used for debugging and
545 * testing. For example, a method that should only be called while
546 * a lock is held can assert that this is the case:
547 *
548 *
 
549 * class X {
550 * ReentrantLock lock = new ReentrantLock();
551 * // ...
552 *
553 * public void m() {
554 * assert lock.isHeldByCurrentThread();
555 * // ... method body
556 * }
557 * }
558 *
559 *
560 *

It can also be used to ensure that a reentrant lock is used

561 * in a non-reentrant manner, for example:
562 *
563 *
 
564 * class X {
565 * ReentrantLock lock = new ReentrantLock();
566 * // ...
567 *
568 * public void m() {
569 * assert !lock.isHeldByCurrentThread();
570 * lock.lock();
571 * try {
572 * // ... method body
573 * } finally {
574 * lock.unlock();
575 * }
576 * }
577 * }
578 *
579 *
580 * @return {@code true} if current thread holds this lock and
581 * {@code false} otherwise
582 */
583 public boolean isHeldByCurrentThread() {
584 return sync.isHeldExclusively();
585 }
586
587 /**
588 * Queries if this lock is held by any thread. This method is
589 * designed for use in monitoring of the system state,
590 * not for synchronization control.
591 *
592 * @return {@code true} if any thread holds this lock and
593 * {@code false} otherwise
594 */
595 public boolean isLocked() {
596 return sync.isLocked();
597 }
598
599 /**
600 * Returns {@code true} if this lock has fairness set true.
601 *
602 * @return {@code true} if this lock has fairness set true
603 */
604 public final boolean isFair() {
605 return sync instanceof FairSync;
606 }
607
608 /**
609 * Returns the thread that currently owns this lock, or
610 * {@code null} if not owned. When this method is called by a
611 * thread that is not the owner, the return value reflects a
612 * best-effort approximation of current lock status. For example,
613 * the owner may be momentarily {@code null} even if there are
614 * threads trying to acquire the lock but have not yet done so.
615 * This method is designed to facilitate construction of
616 * subclasses that provide more extensive lock monitoring
617 * facilities.
618 *
619 * @return the owner, or {@code null} if not owned
620 */
621 protected Thread getOwner() {
622 return sync.getOwner();
623 }
624
625 /**
626 * Queries whether any threads are waiting to acquire this lock. Note that
627 * because cancellations may occur at any time, a {@code true}
628 * return does not guarantee that any other thread will ever
629 * acquire this lock. This method is designed primarily for use in
630 * monitoring of the system state.
631 *
632 * @return {@code true} if there may be other threads waiting to
633 * acquire the lock
634 */
635 public final boolean hasQueuedThreads() {
636 return sync.hasQueuedThreads();
637 }
638
639
640 /**
641 * Queri