533 *
534 * @return the Condition object
535 */
536 public Condition newCondition() {
537 return sync.newCondition();
538 }
539
540 /**
541 * Queries the number of holds on this lock by the current thread.
542 *
543 * <p>A thread has a hold on a lock for each lock action that is not
544 * matched by an unlock action.
545 *
546 * <p>The hold count information is typically only used for testing and
547 * debugging purposes. For example, if a certain section of code should
548 * not be entered with the lock already held then we can assert that
549 * fact:
550 *
551 * <pre> {@code
552 * class X {
553 * ReentrantLock lock = new ReentrantLock();
554 * // ...
555 * public void m() {
556 * assert lock.getHoldCount() == 0;
557 * lock.lock();
558 * try {
559 * // ... method body
560 * } finally {
561 * lock.unlock();
562 * }
563 * }
564 * }}</pre>
565 *
566 * @return the number of holds on this lock by the current thread,
567 * or zero if this lock is not held by the current thread
568 */
569 public int getHoldCount() {
570 return sync.getHoldCount();
571 }
572
573 /**
574 * Queries if this lock is held by the current thread.
575 *
576 * <p>Analogous to the {@link Thread#holdsLock(Object)} method for
577 * built-in monitor locks, this method is typically used for
578 * debugging and testing. For example, a method that should only be
579 * called while a lock is held can assert that this is the case:
580 *
581 * <pre> {@code
582 * class X {
583 * ReentrantLock lock = new ReentrantLock();
584 * // ...
585 *
586 * public void m() {
587 * assert lock.isHeldByCurrentThread();
588 * // ... method body
589 * }
590 * }}</pre>
591 *
592 * <p>It can also be used to ensure that a reentrant lock is used
593 * in a non-reentrant manner, for example:
594 *
595 * <pre> {@code
596 * class X {
597 * ReentrantLock lock = new ReentrantLock();
598 * // ...
599 *
600 * public void m() {
601 * assert !lock.isHeldByCurrentThread();
602 * lock.lock();
603 * try {
604 * // ... method body
605 * } finally {
606 * lock.unlock();
607 * }
608 * }
609 * }}</pre>
610 *
611 * @return {@code true} if current thread holds this lock and
612 * {@code false} otherwise
613 */
614 public boolean isHeldByCurrentThread() {
615 return sync.isHeldExclusively();
616 }
617
|
533 *
534 * @return the Condition object
535 */
536 public Condition newCondition() {
537 return sync.newCondition();
538 }
539
540 /**
541 * Queries the number of holds on this lock by the current thread.
542 *
543 * <p>A thread has a hold on a lock for each lock action that is not
544 * matched by an unlock action.
545 *
546 * <p>The hold count information is typically only used for testing and
547 * debugging purposes. For example, if a certain section of code should
548 * not be entered with the lock already held then we can assert that
549 * fact:
550 *
551 * <pre> {@code
552 * class X {
553 * final ReentrantLock lock = new ReentrantLock();
554 * // ...
555 * public void m() {
556 * assert lock.getHoldCount() == 0;
557 * lock.lock();
558 * try {
559 * // ... method body
560 * } finally {
561 * lock.unlock();
562 * }
563 * }
564 * }}</pre>
565 *
566 * @return the number of holds on this lock by the current thread,
567 * or zero if this lock is not held by the current thread
568 */
569 public int getHoldCount() {
570 return sync.getHoldCount();
571 }
572
573 /**
574 * Queries if this lock is held by the current thread.
575 *
576 * <p>Analogous to the {@link Thread#holdsLock(Object)} method for
577 * built-in monitor locks, this method is typically used for
578 * debugging and testing. For example, a method that should only be
579 * called while a lock is held can assert that this is the case:
580 *
581 * <pre> {@code
582 * class X {
583 * final ReentrantLock lock = new ReentrantLock();
584 * // ...
585 *
586 * public void m() {
587 * assert lock.isHeldByCurrentThread();
588 * // ... method body
589 * }
590 * }}</pre>
591 *
592 * <p>It can also be used to ensure that a reentrant lock is used
593 * in a non-reentrant manner, for example:
594 *
595 * <pre> {@code
596 * class X {
597 * final ReentrantLock lock = new ReentrantLock();
598 * // ...
599 *
600 * public void m() {
601 * assert !lock.isHeldByCurrentThread();
602 * lock.lock();
603 * try {
604 * // ... method body
605 * } finally {
606 * lock.unlock();
607 * }
608 * }
609 * }}</pre>
610 *
611 * @return {@code true} if current thread holds this lock and
612 * {@code false} otherwise
613 */
614 public boolean isHeldByCurrentThread() {
615 return sync.isHeldExclusively();
616 }
617
|