001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003 *
004 * This code is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU General Public License version 2 only, as
006 * published by the Free Software Foundation. Sun designates this
007 * particular file as subject to the "Classpath" exception as provided
008 * by Sun in the LICENSE file that accompanied this code.
009 *
010 * This code is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013 * version 2 for more details (a copy is included in the LICENSE file that
014 * accompanied this code).
015 *
016 * You should have received a copy of the GNU General Public License version
017 * 2 along with this work; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019 *
020 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021 * CA 95054 USA or visit www.sun.com if you need additional information or
022 * have any questions.
023 */
024
025 /*
026 * This file is available under and governed by the GNU General Public
027 * License version 2 only, as published by the Free Software Foundation.
028 * However, the following notice accompanied the original version of this
029 * file:
030 *
031 * Written by Doug Lea with assistance from members of JCP JSR-166
032 * Expert Group and released to the public domain, as explained at
033 * http://creativecommons.org/licenses/publicdomain
034 */
035
036 package java.util.concurrent.locks;
037
038 import java.util.*;
039 import java.util.concurrent.*;
040 import java.util.concurrent.atomic.*;
041
042 /**
043 * A reentrant mutual exclusion {@link Lock} with the same basic
044 * behavior and semantics as the implicit monitor lock accessed using
045 * {@code synchronized} methods and statements, but with extended
046 * capabilities.
047 *
048 * <p>A {@code ReentrantLock} is <em>owned</em> by the thread last
049 * successfully locking, but not yet unlocking it. A thread invoking
050 * {@code lock} will return, successfully acquiring the lock, when
051 * the lock is not owned by another thread. The method will return
052 * immediately if the current thread already owns the lock. This can
053 * be checked using methods {@link #isHeldByCurrentThread}, and {@link
054 * #getHoldCount}.
055 *
056 * <p>The constructor for this class accepts an optional
057 * <em>fairness</em> parameter. When set {@code true}, under
058 * contention, locks favor granting access to the longest-waiting
059 * thread. Otherwise this lock does not guarantee any particular
060 * access order. Programs using fair locks accessed by many threads
061 * may display lower overall throughput (i.e., are slower; often much
062 * slower) than those using the default setting, but have smaller
063 * variances in times to obtain locks and guarantee lack of
064 * starvation. Note however, that fairness of locks does not guarantee
065 * fairness of thread scheduling. Thus, one of many threads using a
066 * fair lock may obtain it multiple times in succession while other
067 * active threads are not progressing and not currently holding the
068 * lock.
069 * Also note that the untimed {@link #tryLock() tryLock} method does not
070 * honor the fairness setting. It will succeed if the lock
071 * is available even if other threads are waiting.
072 *
073 * <p>It is recommended practice to <em>always</em> immediately
074 * follow a call to {@code lock} with a {@code try} block, most
075 * typically in a before/after construction such as:
076 *
077 * <pre>
078 * class X {
079 * private final ReentrantLock lock = new ReentrantLock();
080 * // ...
081 *
082 * public void m() {
083 * lock.lock(); // block until condition holds
084 * try {
085 * // ... method body
086 * } finally {
087 * lock.unlock()
088 * }
089 * }
090 * }
091 * </pre>
092 *
093 * <p>In addition to implementing the {@link Lock} interface, this
094 * class defines methods {@code isLocked} and
095 * {@code getLockQueueLength}, as well as some associated
096 * {@code protected} access methods that may be useful for
097 * instrumentation and monitoring.
098 *
099 * <p>Serialization of this class behaves in the same way as built-in
100 * locks: a deserialized lock is in the unlocked state, regardless of
101 * its state when serialized.
102 *
103 * <p>This lock supports a maximum of 2147483647 recursive locks by
104 * the same thread. Attempts to exceed this limit result in
105 * {@link Error} throws from locking methods.
106 *
107 * @since 1.5
108 * @author Doug Lea
109 */
110 public class ReentrantLock implements Lock, java.io.Serializable {
111 private static final long serialVersionUID = 7373984872572414699L;
112 /** Synchronizer providing all implementation mechanics */
113 private final Sync sync;
114
115 /**
116 * Base of synchronization control for this lock. Subclassed
117 * into fair and nonfair versions below. Uses AQS state to
118 * represent the number of holds on the lock.
119 */
120 static abstract class Sync extends AbstractQueuedSynchronizer {
121 private static final long serialVersionUID = -5179523762034025860L;
122
123 /**
124 * Performs {@link Lock#lock}. The main reason for subclassing
125 * is to allow fast path for nonfair version.
126 */
127 abstract void lock();
128
129 /**
130 * Performs non-fair tryLock. tryAcquire is
131 * implemented in subclasses, but both need nonfair
132 * try for trylock method.
133 */
134 final boolean nonfairTryAcquire(int acquires) {
135 final Thread current = Thread.currentThread();
136 int c = getState();
137 if (c == 0) {
138 if (compareAndSetState(0, acquires)) {
139 setExclusiveOwnerThread(current);
140 return true;
141 }
142 } else if (current == getExclusiveOwnerThread()) {
143 int nextc = c + acquires;
144 if (nextc < 0) // overflow
145 throw new Error("Maximum lock count exceeded");
146 setState(nextc);
147 return true;
148 }
149 return false;
150 }
151
152 protected final boolean tryRelease(int releases) {
153 int c = getState() - releases;
154 if (Thread.currentThread() != getExclusiveOwnerThread())
155 throw new IllegalMonitorStateException();
156 boolean free = false;
157 if (c == 0) {
158 free = true;
159 setExclusiveOwnerThread(null);
160 }
161 setState(c);
162 return free;
163 }
164
165 protected final boolean isHeldExclusively() {
166 // While we must in general read state before owner,
167 // we don't need to do so to check if current thread is owner
168 return getExclusiveOwnerThread() == Thread.currentThread();
169 }
170
171 final ConditionObject newCondition() {
172 return new ConditionObject();
173 }
174
175 // Methods relayed from outer class
176
177 final Thread getOwner() {
178 return getState() == 0 ? null : getExclusiveOwnerThread();
179 }
180
181 final int getHoldCount() {
182 return isHeldExclusively() ? getState() : 0;
183 }
184
185 final boolean isLocked() {
186 return getState() != 0;
187 }
188
189 /**
190 * Reconstitutes this lock instance from a stream.
191 * @param s the stream
192 */
193 private void readObject(java.io.ObjectInputStream s)
194 throws java.io.IOException, ClassNotFoundException {
195 s.defaultReadObject();
196 setState(0); // reset to unlocked state
197 }
198 }
199
200 /**
201 * Sync object for non-fair locks
202 */
203 final static class NonfairSync extends Sync {
204 private static final long serialVersionUID = 7316153563782823691L;
205
206 /**
207 * Performs lock. Try immediate barge, backing up to normal
208 * acquire on failure.
209 */
210 final void lock() {
211 if (compareAndSetState(0, 1))
212 setExclusiveOwnerThread(Thread.currentThread());
213 else
214 acquire(1);
215 }
216
217 protected final boolean tryAcquire(int acquires) {
218 return nonfairTryAcquire(acquires);
219 }
220 }
221
222 /**
223 * Sync object for fair locks
224 */
225 final static class FairSync extends Sync {
226 private static final long serialVersionUID = -3000897897090466540L;
227
228 final void lock() {
229 acquire(1);
230 }
231
232 /**
233 * Fair version of tryAcquire. Don't grant access unless
234 * recursive call or no waiters or is first.
235 */
236 protected final boolean tryAcquire(int acquires) {
237 final Thread current = Thread.currentThread();
238 int c = getState();
239 if (c == 0) {
240 if (!hasQueuedPredecessors()
241 && compareAndSetState(0, acquires)) {
242 setExclusiveOwnerThread(current);
243 return true;
244 }
245 } else if (current == getExclusiveOwnerThread()) {
246 int nextc = c + acquires;
247 if (nextc < 0)
248 throw new Error("Maximum lock count exceeded");
249 setState(nextc);
250 return true;
251 }
252 return false;
253 }
254 }
255
256 /**
257 * Creates an instance of {@code ReentrantLock}.
258 * This is equivalent to using {@code ReentrantLock(false)}.
259 */
260 public ReentrantLock() {
261 sync = new NonfairSync();
262 }
263
264 /**
265 * Creates an instance of {@code ReentrantLock} with the
266 * given fairness policy.
267 *
268 * @param fair {@code true} if this lock should use a fair ordering policy
269 */
270 public ReentrantLock(boolean fair) {
271 sync = (fair) ? new FairSync() : new NonfairSync();
272 }
273
274 /**
275 * Acquires the lock.
276 *
277 * <p>Acquires the lock if it is not held by another thread and returns
278 * immediately, setting the lock hold count to one.
279 *
280 * <p>If the current thread already holds the lock then the hold
281 * count is incremented by one and the method returns immediately.
282 *
283 * <p>If the lock is held by another thread then the
284 * current thread becomes disabled for thread scheduling
285 * purposes and lies dormant until the lock has been acquired,
286 * at which time the lock hold count is set to one.
287 */
288 public void lock() {
289 sync.lock();
290 }
291
292 /**
293 * Acquires the lock unless the current thread is
294 * {@linkplain Thread#interrupt interrupted}.
295 *
296 * <p>Acquires the lock if it is not held by another thread and returns
297 * immediately, setting the lock hold count to one.
298 *
299 * <p>If the current thread already holds this lock then the hold count
300 * is incremented by one and the method returns immediately.
301 *
302 * <p>If the lock is held by another thread then the
303 * current thread becomes disabled for thread scheduling
304 * purposes and lies dormant until one of two things happens:
305 *
306 * <ul>
307 *
308 * <li>The lock is acquired by the current thread; or
309 *
310 * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
311 * current thread.
312 *
313 * </ul>
314 *
315 * <p>If the lock is acquired by the current thread then the lock hold
316 * count is set to one.
317 *
318 * <p>If the current thread:
319 *
320 * <ul>
321 *
322 * <li>has its interrupted status set on entry to this method; or
323 *
324 * <li>is {@linkplain Thread#interrupt interrupted} while acquiring
325 * the lock,
326 *
327 * </ul>
328 *
329 * then {@link InterruptedException} is thrown and the current thread's
330 * interrupted status is cleared.
331 *
332 * <p>In this implementation, as this method is an explicit
333 * interruption point, preference is given to responding to the
334 * interrupt over normal or reentrant acquisition of the lock.
335 *
336 * @throws InterruptedException if the current thread is interrupted
337 */
338 public void lockInterruptibly() throws InterruptedException {
339 sync.acquireInterruptibly(1);
340 }
341
342 /**
343 * Acquires the lock only if it is not held by another thread at the time
344 * of invocation.
345 *
346 * <p>Acquires the lock if it is not held by another thread and
347 * returns immediately with the value {@code true}, setting the
348 * lock hold count to one. Even when this lock has been set to use a
349 * fair ordering policy, a call to {@code tryLock()} <em>will</em>
350 * immediately acquire the lock if it is available, whether or not
351 * other threads are currently waiting for the lock.
352 * This "barging" behavior can be useful in certain
353 * circumstances, even though it breaks fairness. If you want to honor
354 * the fairness setting for this lock, then use
355 * {@link #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS) }
356 * which is almost equivalent (it also detects interruption).
357 *
358 * <p> If the current thread already holds this lock then the hold
359 * count is incremented by one and the method returns {@code true}.
360 *
361 * <p>If the lock is held by another thread then this method will return
362 * immediately with the value {@code false}.
363 *
364 * @return {@code true} if the lock was free and was acquired by the
365 * current thread, or the lock was already held by the current
366 * thread; and {@code false} otherwise
367 */
368 public boolean tryLock() {
369 return sync.nonfairTryAcquire(1);
370 }
371
372 /**
373 * Acquires the lock if it is not held by another thread within the given
374 * waiting time and the current thread has not been
375 * {@linkplain Thread#interrupt interrupted}.
376 *
377 * <p>Acquires the lock if it is not held by another thread and returns
378 * immediately with the value {@code true}, setting the lock hold count
379 * to one. If this lock has been set to use a fair ordering policy then
380 * an available lock <em>will not</em> be acquired if any other threads
381 * are waiting for the lock. This is in contrast to the {@link #tryLock()}
382 * method. If you want a timed {@code tryLock} that does permit barging on
383 * a fair lock then combine the timed and un-timed forms together:
384 *
385 * <pre>if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
386 * </pre>
387 *
388 * <p>If the current thread
389 * already holds this lock then the hold count is incremented by one and
390 * the method returns {@code true}.
391 *
392 * <p>If the lock is held by another thread then the
393 * current thread becomes disabled for thread scheduling
394 * purposes and lies dormant until one of three things happens:
395 *
396 * <ul>
397 *
398 * <li>The lock is acquired by the current thread; or
399 *
400 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
401 * the current thread; or
402 *
403 * <li>The specified waiting time elapses
404 *
405 * </ul>
406 *
407 * <p>If the lock is acquired then the value {@code true} is returned and
408 * the lock hold count is set to one.
409 *
410 * <p>If the current thread:
411 *
412 * <ul>
413 *
414 * <li>has its interrupted status set on entry to this method; or
415 *
416 * <li>is {@linkplain Thread#interrupt interrupted} while
417 * acquiring the lock,
418 *
419 * </ul>
420 * then {@link InterruptedException} is thrown and the current thread's
421 * interrupted status is cleared.
422 *
423 * <p>If the specified waiting time elapses then the value {@code false}
424 * is returned. If the time is less than or equal to zero, the method
425 * will not wait at all.
426 *
427 * <p>In this implementation, as this method is an explicit
428 * interruption point, preference is given to responding to the
429 * interrupt over normal or reentrant acquisition of the lock, and
430 * over reporting the elapse of the waiting time.
431 *
432 * @param timeout the time to wait for the lock
433 * @param unit the time unit of the timeout argument
434 * @return {@code true} if the lock was free and was acquired by the
435 * current thread, or the lock was already held by the current
436 * thread; and {@code false} if the waiting time elapsed before
437 * the lock could be acquired
438 * @throws InterruptedException if the current thread is interrupted
439 * @throws NullPointerException if the time unit is null
440 *
441 */
442 public boolean tryLock(long timeout, TimeUnit unit)
443 throws InterruptedException {
444 return sync.tryAcquireNanos(1, unit.toNanos(timeout));
445 }
446
447 /**
448 * Attempts to release this lock.
449 *
450 * <p>If the current thread is the holder of this lock then the hold
451 * count is decremented. If the hold count is now zero then the lock
452 * is released. If the current thread is not the holder of this
453 * lock then {@link IllegalMonitorStateException} is thrown.
454 *
455 * @throws IllegalMonitorStateException if the current thread does not
456 * hold this lock
457 */
458 public void unlock() {
459 sync.release(1);
460 }
461
462 /**
463 * Returns a {@link Condition} instance for use with this
464 * {@link Lock} instance.
465 *
466 * <p>The returned {@link Condition} instance supports the same
467 * usages as do the {@link Object} monitor methods ({@link
468 * Object#wait() wait}, {@link Object#notify notify}, and {@link
469 * Object#notifyAll notifyAll}) when used with the built-in
470 * monitor lock.
471 *
472 * <ul>
473 *
474 * <li>If this lock is not held when any of the {@link Condition}
475 * {@linkplain Condition#await() waiting} or {@linkplain
476 * Condition#signal signalling} methods are called, then an {@link
477 * IllegalMonitorStateException} is thrown.
478 *
479 * <li>When the condition {@linkplain Condition#await() waiting}
480 * methods are called the lock is released and, before they
481 * return, the lock is reacquired and the lock hold count restored
482 * to what it was when the method was called.
483 *
484 * <li>If a thread is {@linkplain Thread#interrupt interrupted}
485 * while waiting then the wait will terminate, an {@link
486 * InterruptedException} will be thrown, and the thread's
487 * interrupted status will be cleared.
488 *
489 * <li> Waiting threads are signalled in FIFO order.
490 *
491 * <li>The ordering of lock reacquisition for threads returning
492 * from waiting methods is the same as for threads initially
493 * acquiring the lock, which is in the default case not specified,
494 * but for <em>fair</em> locks favors those threads that have been
495 * waiting the longest.
496 *
497 * </ul>
498 *
499 * @return the Condition object
500 */
501 public Condition newCondition() {
502 return sync.newCondition();
503 }
504
505 /**
506 * Queries the number of holds on this lock by the current thread.
507 *
508 * <p>A thread has a hold on a lock for each lock action that is not
509 * matched by an unlock action.
510 *
511 * <p>The hold count information is typically only used for testing and
512 * debugging purposes. For example, if a certain section of code should
513 * not be entered with the lock already held then we can assert that
514 * fact:
515 *
516 * <pre>
517 * class X {
518 * ReentrantLock lock = new ReentrantLock();
519 * // ...
520 * public void m() {
521 * assert lock.getHoldCount() == 0;
522 * lock.lock();
523 * try {
524 * // ... method body
525 * } finally {
526 * lock.unlock();
527 * }
528 * }
529 * }
530 * </pre>
531 *
532 * @return the number of holds on this lock by the current thread,
533 * or zero if this lock is not held by the current thread
534 */
535 public int getHoldCount() {
536 return sync.getHoldCount();
537 }
538
539 /**
540 * Queries if this lock is held by the current thread.
541 *
542 * <p>Analogous to the {@link Thread#holdsLock} method for built-in
543 * monitor locks, this method is typically used for debugging and
544 * testing. For example, a method that should only be called while
545 * a lock is held can assert that this is the case:
546 *
547 * <pre>
548 * class X {
549 * ReentrantLock lock = new ReentrantLock();
550 * // ...
551 *
552 * public void m() {
553 * assert lock.isHeldByCurrentThread();
554 * // ... method body
555 * }
556 * }
557 * </pre>
558 *
559 * <p>It can also be used to ensure that a reentrant lock is used
560 * in a non-reentrant manner, for example:
561 *
562 * <pre>
563 * class X {
564 * ReentrantLock lock = new ReentrantLock();
565 * // ...
566 *
567 * public void m() {
568 * assert !lock.isHeldByCurrentThread();
569 * lock.lock();
570 * try {
571 * // ... method body
572 * } finally {
573 * lock.unlock();
574 * }
575 * }
576 * }
577 * </pre>
578 *
579 * @return {@code true} if current thread holds this lock and
580 * {@code false} otherwise
581 */
582 public boolean isHeldByCurrentThread() {
583 return sync.isHeldExclusively();
584 }
585
586 /**
587 * Queries if this lock is held by any thread. This method is
588 * designed for use in monitoring of the system state,
589 * not for synchronization control.
590 *
591 * @return {@code true} if any thread holds this lock and
592 * {@code false} otherwise
593 */
594 public boolean isLocked() {
595 return sync.isLocked();
596 }
597
598 /**
599 * Returns {@code true} if this lock has fairness set true.
600 *
601 * @return {@code true} if this lock has fairness set true
602 */
603 public final boolean isFair() {
604 return sync instanceof FairSync;
605 }
606
607 /**
608 * Returns the thread that currently owns this lock, or
609 * {@code null} if not owned. When this method is called by a
610 * thread that is not the owner, the return value reflects a
611 * best-effort approximation of current lock status. For example,
612 * the owner may be momentarily {@code null} even if there are
613 * threads trying to acquire the lock but have not yet done so.
614 * This method is designed to facilitate construction of
615 * subclasses that provide more extensive lock monitoring
616 * facilities.
617 *
618 * @return the owner, or {@code null} if not owned
619 */
620 protected Thread getOwner() {
621 return sync.getOwner();
622 }
623
624 /**
625 * Queries whether any threads are waiting to acquire this lock. Note that
626 * because cancellations may occur at any time, a {@code true}
627 * return does not guarantee that any other thread will ever
628 * acquire this lock. This method is designed primarily for use in
629 * monitoring of the system state.
630 *
631 * @return {@code true} if there may be other threads waiting to
632 * acquire the lock
633 */
634 public final boolean hasQueuedThreads() {
635 return sync.hasQueuedThreads();
636 }
637
638 /**
639 * Queries whether the given thread is waiting to acquire this
640 * lock. Note that because cancellations may occur at any time, a
641 * {@code true} return does not guarantee that this thread
642 * will ever acquire this lock. This method is designed primarily for use
643 * in monitoring of the system state.
644 *
645 * @param thread the thread
646 * @return {@code true} if the given thread is queued waiting for this lock
647 * @throws NullPointerException if the thread is null
648 */
649 public final boolean hasQueuedThread(Thread thread) {
650 return sync.isQueued(thread);
651 }
652
653 /**
654 * Returns an estimate of the number of threads waiting to
655 * acquire this lock. The value is only an estimate because the number of
656 * threads may change dynamically while this method traverses
657 * internal data structures. This method is designed for use in
658 * monitoring of the system state, not for synchronization
659 * control.
660 *
661 * @return the estimated number of threads waiting for this lock
662 */
663 public final int getQueueLength() {
664 return sync.getQueueLength();
665 }
666
667 /**
668 * Returns a collection containing threads that may be waiting to
669 * acquire this lock. Because the actual set of threads may change
670 * dynamically while constructing this result, the returned
671 * collection is only a best-effort estimate. The elements of the
672 * returned collection are in no particular order. This method is
673 * designed to facilitate construction of subclasses that provide
674 * more extensive monitoring facilities.
675 *
676 * @return the collection of threads
677 */
678 protected Collection<Thread> getQueuedThreads() {
679 return sync.getQueuedThreads();
680 }
681
682 /**
683 * Queries whether any threads are waiting on the given condition
684 * associated with this lock. Note that because timeouts and
685 * interrupts may occur at any time, a {@code true} return does
686 * not guarantee that a future {@code signal} will awaken any
687 * threads. This method is designed primarily for use in
688 * monitoring of the system state.
689 *
690 * @param condition the condition
691 * @return {@code true} if there are any waiting threads
692 * @throws IllegalMonitorStateException if this lock is not held
693 * @throws IllegalArgumentException if the given condition is
694 * not associated with this lock
695 * @throws NullPointerException if the condition is null
696 */
697 public boolean hasWaiters(Condition condition) {
698 if (condition == null)
699 throw new NullPointerException();
700 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
701 throw new IllegalArgumentException("not owner");
702 return sync
703 .hasWaiters((AbstractQueuedSynchronizer.ConditionObject) condition);
704 }
705
706 /**
707 * Returns an estimate of the number of threads waiting on the
708 * given condition associated with this lock. Note that because
709 * timeouts and interrupts may occur at any time, the estimate
710 * serves only as an upper bound on the actual number of waiters.
711 * This method is designed for use in monitoring of the system
712 * state, not for synchronization control.
713 *
714 * @param condition the condition
715 * @return the estimated number of waiting threads
716 * @throws IllegalMonitorStateException if this lock is not held
717 * @throws IllegalArgumentException if the given condition is
718 * not associated with this lock
719 * @throws NullPointerException if the condition is null
720 */
721 public int getWaitQueueLength(Condition condition) {
722 if (condition == null)
723 throw new NullPointerException();
724 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
725 throw new IllegalArgumentException("not owner");
726 return sync
727 .getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject) condition);
728 }
729
730 /**
731 * Returns a collection containing those threads that may be
732 * waiting on the given condition associated with this lock.
733 * Because the actual set of threads may change dynamically while
734 * constructing this result, the returned collection is only a
735 * best-effort estimate. The elements of the returned collection
736 * are in no particular order. This method is designed to
737 * facilitate construction of subclasses that provide more
738 * extensive condition monitoring facilities.
739 *
740 * @param condition the condition
741 * @return the collection of threads
742 * @throws IllegalMonitorStateException if this lock is not held
743 * @throws IllegalArgumentException if the given condition is
744 * not associated with this lock
745 * @throws NullPointerException if the condition is null
746 */
747 protected Collection<Thread> getWaitingThreads(Condition condition) {
748 if (condition == null)
749 throw new NullPointerException();
750 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
751 throw new IllegalArgumentException("not owner");
752 return sync
753 .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()
767 + ((o == null) ? "[Unlocked]" : "[Locked by thread "
768 + o.getName() + "]");
769 }
770 }
|