1. /*
  2. * @(#)ReentrantLock.java 1.7 04/07/13
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.concurrent.locks;
  8. import java.util.*;
  9. import java.util.concurrent.*;
  10. import java.util.concurrent.atomic.*;
  11. /**
  12. * A reentrant mutual exclusion {@link Lock} with the same basic
  13. * behavior and semantics as the implicit monitor lock accessed using
  14. * <tt>synchronized</tt> methods and statements, but with extended
  15. * capabilities.
  16. *
  17. * <p> A <tt>ReentrantLock</tt> is <em>owned</em> by the thread last
  18. * successfully locking, but not yet unlocking it. A thread invoking
  19. * <tt>lock</tt> will return, successfully acquiring the lock, when
  20. * the lock is not owned by another thread. The method will return
  21. * immediately if the current thread already owns the lock. This can
  22. * be checked using methods {@link #isHeldByCurrentThread}, and {@link
  23. * #getHoldCount}.
  24. *
  25. * <p> The constructor for this class accepts an optional
  26. * <em>fairness</em> parameter. When set <tt>true</tt>, under
  27. * contention, locks favor granting access to the longest-waiting
  28. * thread. Otherwise this lock does not guarantee any particular
  29. * access order. Programs using fair locks accessed by many threads
  30. * may display lower overall throughput (i.e., are slower; often much
  31. * slower) than those using the default setting, but have smaller
  32. * variances in times to obtain locks and guarantee lack of
  33. * starvation. Note however, that fairness of locks does not guarantee
  34. * fairness of thread scheduling. Thus, one of many threads using a
  35. * fair lock may obtain it multiple times in succession while other
  36. * active threads are not progressing and not currently holding the
  37. * lock.
  38. * Also note that the untimed {@link #tryLock() tryLock} method does not
  39. * honor the fairness setting. It will succeed if the lock
  40. * is available even if other threads are waiting.
  41. *
  42. * <p> It is recommended practice to <em>always</em> immediately
  43. * follow a call to <tt>lock</tt> with a <tt>try</tt> block, most
  44. * typically in a before/after construction such as:
  45. *
  46. * <pre>
  47. * class X {
  48. * private final ReentrantLock lock = new ReentrantLock();
  49. * // ...
  50. *
  51. * public void m() {
  52. * lock.lock(); // block until condition holds
  53. * try {
  54. * // ... method body
  55. * } finally {
  56. * lock.unlock()
  57. * }
  58. * }
  59. * }
  60. * </pre>
  61. *
  62. * <p>In addition to implementing the {@link Lock} interface, this
  63. * class defines methods <tt>isLocked</tt> and
  64. * <tt>getLockQueueLength</tt>, as well as some associated
  65. * <tt>protected</tt> access methods that may be useful for
  66. * instrumentation and monitoring.
  67. *
  68. * <p> Serialization of this class behaves in the same way as built-in
  69. * locks: a deserialized lock is in the unlocked state, regardless of
  70. * its state when serialized.
  71. *
  72. * <p> This lock supports a maximum of 2147483648 recursive locks by
  73. * the same thread.
  74. *
  75. * @since 1.5
  76. * @author Doug Lea
  77. *
  78. */
  79. public class ReentrantLock implements Lock, java.io.Serializable {
  80. private static final long serialVersionUID = 7373984872572414699L;
  81. /** Synchronizer providing all implementation mechanics */
  82. private final Sync sync;
  83. /**
  84. * Base of synchronization control for this lock. Subclassed
  85. * into fair and nonfair versions below. Uses AQS state to
  86. * represent the number of holds on the lock.
  87. */
  88. static abstract class Sync extends AbstractQueuedSynchronizer {
  89. /** Current owner thread */
  90. transient Thread owner;
  91. /**
  92. * Perform {@link Lock#lock}. The main reason for subclassing
  93. * is to allow fast path for nonfair version.
  94. */
  95. abstract void lock();
  96. /**
  97. * Perform non-fair tryLock. tryAcquire is
  98. * implemented in subclasses, but both need nonfair
  99. * try for trylock method
  100. */
  101. final boolean nonfairTryAcquire(int acquires) {
  102. final Thread current = Thread.currentThread();
  103. int c = getState();
  104. if (c == 0) {
  105. if (compareAndSetState(0, acquires)) {
  106. owner = current;
  107. return true;
  108. }
  109. }
  110. else if (current == owner) {
  111. setState(c+acquires);
  112. return true;
  113. }
  114. return false;
  115. }
  116. protected final boolean tryRelease(int releases) {
  117. int c = getState() - releases;
  118. if (Thread.currentThread() != owner)
  119. throw new IllegalMonitorStateException();
  120. boolean free = false;
  121. if (c == 0) {
  122. free = true;
  123. owner = null;
  124. }
  125. setState(c);
  126. return free;
  127. }
  128. protected final boolean isHeldExclusively() {
  129. return getState() != 0 && owner == Thread.currentThread();
  130. }
  131. final ConditionObject newCondition() {
  132. return new ConditionObject();
  133. }
  134. // Methods relayed from outer class
  135. final Thread getOwner() {
  136. int c = getState();
  137. Thread o = owner;
  138. return (c == 0)? null : o;
  139. }
  140. final int getHoldCount() {
  141. int c = getState();
  142. Thread o = owner;
  143. return (o == Thread.currentThread())? c : 0;
  144. }
  145. final boolean isLocked() {
  146. return getState() != 0;
  147. }
  148. /**
  149. * Reconstitute this lock instance from a stream
  150. * @param s the stream
  151. */
  152. private void readObject(java.io.ObjectInputStream s)
  153. throws java.io.IOException, ClassNotFoundException {
  154. s.defaultReadObject();
  155. setState(0); // reset to unlocked state
  156. }
  157. }
  158. /**
  159. * Sync object for non-fair locks
  160. */
  161. final static class NonfairSync extends Sync {
  162. /**
  163. * Perform lock. Try immediate barge, backing up to normal
  164. * acquire on failure.
  165. */
  166. final void lock() {
  167. if (compareAndSetState(0, 1))
  168. owner = Thread.currentThread();
  169. else
  170. acquire(1);
  171. }
  172. protected final boolean tryAcquire(int acquires) {
  173. return nonfairTryAcquire(acquires);
  174. }
  175. }
  176. /**
  177. * Sync object for fair locks
  178. */
  179. final static class FairSync extends Sync {
  180. final void lock() {
  181. acquire(1);
  182. }
  183. /**
  184. * Fair version of tryAcquire. Don't grant access unless
  185. * recursive call or no waiters or is first.
  186. */
  187. protected final boolean tryAcquire(int acquires) {
  188. final Thread current = Thread.currentThread();
  189. int c = getState();
  190. if (c == 0) {
  191. Thread first = getFirstQueuedThread();
  192. if ((first == null || first == current) &&
  193. compareAndSetState(0, acquires)) {
  194. owner = current;
  195. return true;
  196. }
  197. }
  198. else if (current == owner) {
  199. setState(c+acquires);
  200. return true;
  201. }
  202. return false;
  203. }
  204. }
  205. /**
  206. * Creates an instance of <tt>ReentrantLock</tt>.
  207. * This is equivalent to using <tt>ReentrantLock(false)</tt>.
  208. */
  209. public ReentrantLock() {
  210. sync = new NonfairSync();
  211. }
  212. /**
  213. * Creates an instance of <tt>ReentrantLock</tt> with the
  214. * given fairness policy.
  215. * @param fair true if this lock will be fair; else false
  216. */
  217. public ReentrantLock(boolean fair) {
  218. sync = (fair)? new FairSync() : new NonfairSync();
  219. }
  220. /**
  221. * Acquires the lock.
  222. *
  223. * <p>Acquires the lock if it is not held by another thread and returns
  224. * immediately, setting the lock hold count to one.
  225. *
  226. * <p>If the current thread
  227. * already holds the lock then the hold count is incremented by one and
  228. * the method returns immediately.
  229. *
  230. * <p>If the lock is held by another thread then the
  231. * current thread becomes disabled for thread scheduling
  232. * purposes and lies dormant until the lock has been acquired,
  233. * at which time the lock hold count is set to one.
  234. */
  235. public void lock() {
  236. sync.lock();
  237. }
  238. /**
  239. * Acquires the lock unless the current thread is
  240. * {@link Thread#interrupt interrupted}.
  241. *
  242. * <p>Acquires the lock if it is not held by another thread and returns
  243. * immediately, setting the lock hold count to one.
  244. *
  245. * <p>If the current thread already holds this lock then the hold count
  246. * is incremented by one and the method returns immediately.
  247. *
  248. * <p>If the lock is held by another thread then the
  249. * current thread becomes disabled for thread scheduling
  250. * purposes and lies dormant until one of two things happens:
  251. *
  252. * <ul>
  253. *
  254. * <li>The lock is acquired by the current thread; or
  255. *
  256. * <li>Some other thread {@link Thread#interrupt interrupts} the current
  257. * thread.
  258. *
  259. * </ul>
  260. *
  261. * <p>If the lock is acquired by the current thread then the lock hold
  262. * count is set to one.
  263. *
  264. * <p>If the current thread:
  265. *
  266. * <ul>
  267. *
  268. * <li>has its interrupted status set on entry to this method; or
  269. *
  270. * <li>is {@link Thread#interrupt interrupted} while acquiring
  271. * the lock,
  272. *
  273. * </ul>
  274. *
  275. * then {@link InterruptedException} is thrown and the current thread's
  276. * interrupted status is cleared.
  277. *
  278. * <p>In this implementation, as this method is an explicit interruption
  279. * point, preference is
  280. * given to responding to the interrupt over normal or reentrant
  281. * acquisition of the lock.
  282. *
  283. * @throws InterruptedException if the current thread is interrupted
  284. */
  285. public void lockInterruptibly() throws InterruptedException {
  286. sync.acquireInterruptibly(1);
  287. }
  288. /**
  289. * Acquires the lock only if it is not held by another thread at the time
  290. * of invocation.
  291. *
  292. * <p>Acquires the lock if it is not held by another thread and
  293. * returns immediately with the value <tt>true</tt>, setting the
  294. * lock hold count to one. Even when this lock has been set to use a
  295. * fair ordering policy, a call to <tt>tryLock()</tt> <em>will</em>
  296. * immediately acquire the lock if it is available, whether or not
  297. * other threads are currently waiting for the lock.
  298. * This "barging" behavior can be useful in certain
  299. * circumstances, even though it breaks fairness. If you want to honor
  300. * the fairness setting for this lock, then use
  301. * {@link #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS) }
  302. * which is almost equivalent (it also detects interruption).
  303. *
  304. * <p> If the current thread
  305. * already holds this lock then the hold count is incremented by one and
  306. * the method returns <tt>true</tt>.
  307. *
  308. * <p>If the lock is held by another thread then this method will return
  309. * immediately with the value <tt>false</tt>.
  310. *
  311. * @return <tt>true</tt> if the lock was free and was acquired by the
  312. * current thread, or the lock was already held by the current thread; and
  313. * <tt>false</tt> otherwise.
  314. */
  315. public boolean tryLock() {
  316. return sync.nonfairTryAcquire(1);
  317. }
  318. /**
  319. * Acquires the lock if it is not held by another thread within the given
  320. * waiting time and the current thread has not been
  321. * {@link Thread#interrupt interrupted}.
  322. *
  323. * <p>Acquires the lock if it is not held by another thread and returns
  324. * immediately with the value <tt>true</tt>, setting the lock hold count
  325. * to one. If this lock has been set to use a fair ordering policy then
  326. * an available lock <em>will not</em> be acquired if any other threads
  327. * are waiting for the lock. This is in contrast to the {@link #tryLock()}
  328. * method. If you want a timed <tt>tryLock</tt> that does permit barging on
  329. * a fair lock then combine the timed and un-timed forms together:
  330. *
  331. * <pre>if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
  332. * </pre>
  333. *
  334. * <p>If the current thread
  335. * already holds this lock then the hold count is incremented by one and
  336. * the method returns <tt>true</tt>.
  337. *
  338. * <p>If the lock is held by another thread then the
  339. * current thread becomes disabled for thread scheduling
  340. * purposes and lies dormant until one of three things happens:
  341. *
  342. * <ul>
  343. *
  344. * <li>The lock is acquired by the current thread; or
  345. *
  346. * <li>Some other thread {@link Thread#interrupt interrupts} the current
  347. * thread; or
  348. *
  349. * <li>The specified waiting time elapses
  350. *
  351. * </ul>
  352. *
  353. * <p>If the lock is acquired then the value <tt>true</tt> is returned and
  354. * the lock hold count is set to one.
  355. *
  356. * <p>If the current thread:
  357. *
  358. * <ul>
  359. *
  360. * <li>has its interrupted status set on entry to this method; or
  361. *
  362. * <li>is {@link Thread#interrupt interrupted} while acquiring
  363. * the lock,
  364. *
  365. * </ul>
  366. * then {@link InterruptedException} is thrown and the current thread's
  367. * interrupted status is cleared.
  368. *
  369. * <p>If the specified waiting time elapses then the value <tt>false</tt>
  370. * is returned.
  371. * If the time is
  372. * less than or equal to zero, the method will not wait at all.
  373. *
  374. * <p>In this implementation, as this method is an explicit interruption
  375. * point, preference is
  376. * given to responding to the interrupt over normal or reentrant
  377. * acquisition of the lock, and over reporting the elapse of the waiting
  378. * time.
  379. *
  380. * @param timeout the time to wait for the lock
  381. * @param unit the time unit of the timeout argument
  382. *
  383. * @return <tt>true</tt> if the lock was free and was acquired by the
  384. * current thread, or the lock was already held by the current thread; and
  385. * <tt>false</tt> if the waiting time elapsed before the lock could be
  386. * acquired.
  387. *
  388. * @throws InterruptedException if the current thread is interrupted
  389. * @throws NullPointerException if unit is null
  390. *
  391. */
  392. public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
  393. return sync.tryAcquireNanos(1, unit.toNanos(timeout));
  394. }
  395. /**
  396. * Attempts to release this lock.
  397. *
  398. * <p>If the current thread is the
  399. * holder of this lock then the hold count is decremented. If the
  400. * hold count is now zero then the lock is released. If the
  401. * current thread is not the holder of this lock then {@link
  402. * IllegalMonitorStateException} is thrown.
  403. * @throws IllegalMonitorStateException if the current thread does not
  404. * hold this lock.
  405. */
  406. public void unlock() {
  407. sync.release(1);
  408. }
  409. /**
  410. * Returns a {@link Condition} instance for use with this
  411. * {@link Lock} instance.
  412. *
  413. * <p>The returned {@link Condition} instance supports the same
  414. * usages as do the {@link Object} monitor methods ({@link
  415. * Object#wait() wait}, {@link Object#notify notify}, and {@link
  416. * Object#notifyAll notifyAll}) when used with the built-in
  417. * monitor lock.
  418. *
  419. * <ul>
  420. *
  421. * <li>If this lock is not held when any of the {@link Condition}
  422. * {@link Condition#await() waiting} or {@link Condition#signal
  423. * signalling} methods are called, then an {@link
  424. * IllegalMonitorStateException} is thrown.
  425. *
  426. * <li>When the condition {@link Condition#await() waiting}
  427. * methods are called the lock is released and, before they
  428. * return, the lock is reacquired and the lock hold count restored
  429. * to what it was when the method was called.
  430. *
  431. * <li>If a thread is {@link Thread#interrupt interrupted} while
  432. * waiting then the wait will terminate, an {@link
  433. * InterruptedException} will be thrown, and the thread's
  434. * interrupted status will be cleared.
  435. *
  436. * <li> Waiting threads are signalled in FIFO order
  437. *
  438. * <li>The ordering of lock reacquisition for threads returning
  439. * from waiting methods is the same as for threads initially
  440. * acquiring the lock, which is in the default case not specified,
  441. * but for <em>fair</em> locks favors those threads that have been
  442. * waiting the longest.
  443. *
  444. * </ul>
  445. *
  446. * @return the Condition object
  447. */
  448. public Condition newCondition() {
  449. return sync.newCondition();
  450. }
  451. /**
  452. * Queries the number of holds on this lock by the current thread.
  453. *
  454. * <p>A thread has a hold on a lock for each lock action that is not
  455. * matched by an unlock action.
  456. *
  457. * <p>The hold count information is typically only used for testing and
  458. * debugging purposes. For example, if a certain section of code should
  459. * not be entered with the lock already held then we can assert that
  460. * fact:
  461. *
  462. * <pre>
  463. * class X {
  464. * ReentrantLock lock = new ReentrantLock();
  465. * // ...
  466. * public void m() {
  467. * assert lock.getHoldCount() == 0;
  468. * lock.lock();
  469. * try {
  470. * // ... method body
  471. * } finally {
  472. * lock.unlock();
  473. * }
  474. * }
  475. * }
  476. * </pre>
  477. *
  478. * @return the number of holds on this lock by the current thread,
  479. * or zero if this lock is not held by the current thread.
  480. */
  481. public int getHoldCount() {
  482. return sync.getHoldCount();
  483. }
  484. /**
  485. * Queries if this lock is held by the current thread.
  486. *
  487. * <p>Analogous to the {@link Thread#holdsLock} method for built-in
  488. * monitor locks, this method is typically used for debugging and
  489. * testing. For example, a method that should only be called while
  490. * a lock is held can assert that this is the case:
  491. *
  492. * <pre>
  493. * class X {
  494. * ReentrantLock lock = new ReentrantLock();
  495. * // ...
  496. *
  497. * public void m() {
  498. * assert lock.isHeldByCurrentThread();
  499. * // ... method body
  500. * }
  501. * }
  502. * </pre>
  503. *
  504. * <p>It can also be used to ensure that a reentrant lock is used
  505. * in a non-reentrant manner, for example:
  506. *
  507. * <pre>
  508. * class X {
  509. * ReentrantLock lock = new ReentrantLock();
  510. * // ...
  511. *
  512. * public void m() {
  513. * assert !lock.isHeldByCurrentThread();
  514. * lock.lock();
  515. * try {
  516. * // ... method body
  517. * } finally {
  518. * lock.unlock();
  519. * }
  520. * }
  521. * }
  522. * </pre>
  523. * @return <tt>true</tt> if current thread holds this lock and
  524. * <tt>false</tt> otherwise.
  525. */
  526. public boolean isHeldByCurrentThread() {
  527. return sync.isHeldExclusively();
  528. }
  529. /**
  530. * Queries if this lock is held by any thread. This method is
  531. * designed for use in monitoring of the system state,
  532. * not for synchronization control.
  533. * @return <tt>true</tt> if any thread holds this lock and
  534. * <tt>false</tt> otherwise.
  535. */
  536. public boolean isLocked() {
  537. return sync.isLocked();
  538. }
  539. /**
  540. * Returns true if this lock has fairness set true.
  541. * @return true if this lock has fairness set true.
  542. */
  543. public final boolean isFair() {
  544. return sync instanceof FairSync;
  545. }
  546. /**
  547. * Returns the thread that currently owns this lock, or
  548. * <tt>null</tt> if not owned. Note that the owner may be
  549. * momentarily <tt>null</tt> even if there are threads trying to
  550. * acquire the lock but have not yet done so. This method is
  551. * designed to facilitate construction of subclasses that provide
  552. * more extensive lock monitoring facilities.
  553. * @return the owner, or <tt>null</tt> if not owned.
  554. */
  555. protected Thread getOwner() {
  556. return sync.getOwner();
  557. }
  558. /**
  559. * Queries whether any threads are waiting to acquire this lock. Note that
  560. * because cancellations may occur at any time, a <tt>true</tt>
  561. * return does not guarantee that any other thread will ever
  562. * acquire this lock. This method is designed primarily for use in
  563. * monitoring of the system state.
  564. *
  565. * @return true if there may be other threads waiting to acquire
  566. * the lock.
  567. */
  568. public final boolean hasQueuedThreads() {
  569. return sync.hasQueuedThreads();
  570. }
  571. /**
  572. * Queries whether the given thread is waiting to acquire this
  573. * lock. Note that because cancellations may occur at any time, a
  574. * <tt>true</tt> return does not guarantee that this thread
  575. * will ever acquire this lock. This method is designed primarily for use
  576. * in monitoring of the system state.
  577. *
  578. * @param thread the thread
  579. * @return true if the given thread is queued waiting for this lock.
  580. * @throws NullPointerException if thread is null
  581. */
  582. public final boolean hasQueuedThread(Thread thread) {
  583. return sync.isQueued(thread);
  584. }
  585. /**
  586. * Returns an estimate of the number of threads waiting to
  587. * acquire this lock. The value is only an estimate because the number of
  588. * threads may change dynamically while this method traverses
  589. * internal data structures. This method is designed for use in
  590. * monitoring of the system state, not for synchronization
  591. * control.
  592. * @return the estimated number of threads waiting for this lock
  593. */
  594. public final int getQueueLength() {
  595. return sync.getQueueLength();
  596. }
  597. /**
  598. * Returns a collection containing threads that may be waiting to
  599. * acquire this lock. Because the actual set of threads may change
  600. * dynamically while constructing this result, the returned
  601. * collection is only a best-effort estimate. The elements of the
  602. * returned collection are in no particular order. This method is
  603. * designed to facilitate construction of subclasses that provide
  604. * more extensive monitoring facilities.
  605. * @return the collection of threads
  606. */
  607. protected Collection<Thread> getQueuedThreads() {
  608. return sync.getQueuedThreads();
  609. }
  610. /**
  611. * Queries whether any threads are waiting on the given condition
  612. * associated with this lock. Note that because timeouts and
  613. * interrupts may occur at any time, a <tt>true</tt> return does
  614. * not guarantee that a future <tt>signal</tt> will awaken any
  615. * threads. This method is designed primarily for use in
  616. * monitoring of the system state.
  617. * @param condition the condition
  618. * @return <tt>true</tt> if there are any waiting threads.
  619. * @throws IllegalMonitorStateException if this lock
  620. * is not held
  621. * @throws IllegalArgumentException if the given condition is
  622. * not associated with this lock
  623. * @throws NullPointerException if condition null
  624. */
  625. public boolean hasWaiters(Condition condition) {
  626. if (condition == null)
  627. throw new NullPointerException();
  628. if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
  629. throw new IllegalArgumentException("not owner");
  630. return sync.hasWaiters((AbstractQueuedSynchronizer.ConditionObject)condition);
  631. }
  632. /**
  633. * Returns an estimate of the number of threads waiting on the
  634. * given condition associated with this lock. Note that because
  635. * timeouts and interrupts may occur at any time, the estimate
  636. * serves only as an upper bound on the actual number of waiters.
  637. * This method is designed for use in monitoring of the system
  638. * state, not for synchronization control.
  639. * @param condition the condition
  640. * @return the estimated number of waiting threads.
  641. * @throws IllegalMonitorStateException if this lock
  642. * is not held
  643. * @throws IllegalArgumentException if the given condition is
  644. * not associated with this lock
  645. * @throws NullPointerException if condition null
  646. */
  647. public int getWaitQueueLength(Condition condition) {
  648. if (condition == null)
  649. throw new NullPointerException();
  650. if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
  651. throw new IllegalArgumentException("not owner");
  652. return sync.getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject)condition);
  653. }
  654. /**
  655. * Returns a collection containing those threads that may be
  656. * waiting on the given condition associated with this lock.
  657. * Because the actual set of threads may change dynamically while
  658. * constructing this result, the returned collection is only a
  659. * best-effort estimate. The elements of the returned collection
  660. * are in no particular order. This method is designed to
  661. * facilitate construction of subclasses that provide more
  662. * extensive condition monitoring facilities.
  663. * @param condition the condition
  664. * @return the collection of threads
  665. * @throws IllegalMonitorStateException if this lock
  666. * is not held
  667. * @throws IllegalArgumentException if the given condition is
  668. * not associated with this lock
  669. * @throws NullPointerException if condition null
  670. */
  671. protected Collection<Thread> getWaitingThreads(Condition condition) {
  672. if (condition == null)
  673. throw new NullPointerException();
  674. if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
  675. throw new IllegalArgumentException("not owner");
  676. return sync.getWaitingThreads((AbstractQueuedSynchronizer.ConditionObject)condition);
  677. }
  678. /**
  679. * Returns a string identifying this lock, as well as its lock
  680. * state. The state, in brackets, includes either the String
  681. * "Unlocked" or the String "Locked by"
  682. * followed by the {@link Thread#getName} of the owning thread.
  683. * @return a string identifying this lock, as well as its lock state.
  684. */
  685. public String toString() {
  686. Thread owner = sync.getOwner();
  687. return super.toString() + ((owner == null) ?
  688. "[Unlocked]" :
  689. "[Locked by thread " + owner.getName() + "]");
  690. }
  691. }