Java多线程系列--“JUC锁”05之 非公平锁(六)
ck.
450 *
451 *
If the current thread is the holder of this lock then the hold
452 * count is decremented. If the hold count is now zero then the lock
453 * is released. If the current thread is not the holder of this
454 * lock then {@link IllegalMonitorStateException} is thrown.
455 *
456 * @throws IllegalMonitorStateException if the current thread does not
457 * hold this lock
458 */
459 public void unlock() {
460 sync.release(1);
461 }
462
463 /**
464 * Returns a {@link Condition} instance for use with this
465 * {@link Lock} instance.
466 *
467 *
The returned {@link Condition} instance supports the same
468 * usages as do the {@link Object} monitor methods ({@link
469 * Object#wait() wait}, {@link Object#notify notify}, and {@link
470 * Object#notifyAll notifyAll}) when used with the built-in
471 * monitor lock.
472 *
473 *
474 *
475 * If this lock is not held when any of the {@link Condition}
476 * {@linkplain Condition#await() waiting} or {@linkplain
477 * Condition#signal signalling} methods are called, then an {@link
478 * IllegalMonitorStateException} is thrown.
479 *
480 * When the condition {@linkplain Condition#await() waiting}
481 * methods are called the lock is released and, before they
482 * return, the lock is reacquired and the lock hold count restored
483 * to what it was when the method was called.
484 *
485 * If a thread is {@linkplain Thread#interrupt interrupted}
486 * while waiting then the wait will terminate, an {@link
487 * InterruptedException} will be thrown, and the thread's
488 * interrupted status will be cleared.
489 *
490 * Waiting threads are signalled in FIFO order.
491 *
492 * The ordering of lock reacquisition for threads returning
493 * from waiting methods is the same as for threads initially
494 * acquiring the lock, which is in the default case not specified,
495 * but for fair locks favors those threads that have been
496 * waiting the longest.
497 *
498 *
499 *
500 * @return the Condition object
501 */
502 public Condition newCondition() {
503 return sync.newCondition();
504 }
505
506 /**
507 * Queries the number of holds on this lock by the current thread.
508 *
509 *
A thread has a hold on a lock for each lock action that is not
510 * matched by an unlock action.
511 *
512 *
The hold count information is typically only used for testing and
513 * debugging purposes. For example, if a certain section of code should
514 * not be entered with the lock already held then we can assert that
515 * fact:
516 *
517 *
518 * class X {
519 * ReentrantLock lock = new ReentrantLock();
520 * // ...
521 * public void m() {
522 * assert lock.getHoldCount() == 0;
523 * lock.lock();
524 * try {
525 * // ... method body
526 * } finally {
527 * lock.unlock();
528 * }
529 * }
530 * }
531 *
532 *
533 * @return the number of holds on this lock by the current thread,
534 * or zero if this lock is not held by the current thread
535 */
536 public int getHoldCount() {
537 return sync.getHoldCount();
538 }
539
540 /**
541