2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
7 package java.util.concurrent.locks;
9 import java.util.concurrent.*;
10 import java.util.concurrent.atomic.*;
13 * A reentrant mutual exclusion {@link Lock} with the same basic
14 * behavior and semantics as the implicit monitor lock accessed using
15 * {@code synchronized} methods and statements, but with extended
18 * <p>A {@code ReentrantLock} is <em>owned</em> by the thread last
19 * successfully locking, but not yet unlocking it. A thread invoking
20 * {@code lock} will return, successfully acquiring the lock, when
21 * the lock is not owned by another thread. The method will return
22 * immediately if the current thread already owns the lock. This can
23 * be checked using methods {@link #isHeldByCurrentThread}, and {@link
26 * <p>The constructor for this class accepts an optional
27 * <em>fairness</em> parameter. When set {@code true}, under
28 * contention, locks favor granting access to the longest-waiting
29 * thread. Otherwise this lock does not guarantee any particular
30 * access order. Programs using fair locks accessed by many threads
31 * may display lower overall throughput (i.e., are slower; often much
32 * slower) than those using the default setting, but have smaller
33 * variances in times to obtain locks and guarantee lack of
34 * starvation. Note however, that fairness of locks does not guarantee
35 * fairness of thread scheduling. Thus, one of many threads using a
36 * fair lock may obtain it multiple times in succession while other
37 * active threads are not progressing and not currently holding the
39 * Also note that the untimed {@link #tryLock() tryLock} method does not
40 * honor the fairness setting. It will succeed if the lock
41 * is available even if other threads are waiting.
43 * <p>It is recommended practice to <em>always</em> immediately
44 * follow a call to {@code lock} with a {@code try} block, most
45 * typically in a before/after construction such as:
49 * private final ReentrantLock lock = new ReentrantLock();
53 * lock.lock(); // block until condition holds
63 * <p>In addition to implementing the {@link Lock} interface, this
64 * class defines methods {@code isLocked} and
65 * {@code getLockQueueLength}, as well as some associated
66 * {@code protected} access methods that may be useful for
67 * instrumentation and monitoring.
69 * <p>Serialization of this class behaves in the same way as built-in
70 * locks: a deserialized lock is in the unlocked state, regardless of
71 * its state when serialized.
73 * <p>This lock supports a maximum of 2147483647 recursive locks by
74 * the same thread. Attempts to exceed this limit result in
75 * {@link Error} throws from locking methods.
80 public class ReentrantLock implements Lock, java.io.Serializable {
81 private static final long serialVersionUID = 7373984872572414699L;
82 /** Synchronizer providing all implementation mechanics */
83 private final Sync sync;
86 * Base of synchronization control for this lock. Subclassed
87 * into fair and nonfair versions below. Uses AQS state to
88 * represent the number of holds on the lock.
90 static abstract class Sync extends AbstractQueuedSynchronizer {
91 private static final long serialVersionUID = -5179523762034025860L;
94 * Performs {@link Lock#lock}. The main reason for subclassing
95 * is to allow fast path for nonfair version.
100 * Performs non-fair tryLock. tryAcquire is
101 * implemented in subclasses, but both need nonfair
102 * try for trylock method.
104 final boolean nonfairTryAcquire(int acquires) {
105 final Thread current = Thread.currentThread();
108 if (compareAndSetState(0, acquires)) {
109 setExclusiveOwnerThread(current);
113 else if (current == getExclusiveOwnerThread()) {
114 int nextc = c + acquires;
115 if (nextc < 0) // overflow
116 throw new Error("Maximum lock count exceeded");
123 protected final boolean tryRelease(int releases) {
124 int c = getState() - releases;
125 if (Thread.currentThread() != getExclusiveOwnerThread())
126 throw new IllegalMonitorStateException();
127 boolean free = false;
130 setExclusiveOwnerThread(null);
136 protected final boolean isHeldExclusively() {
137 // While we must in general read state before owner,
138 // we don't need to do so to check if current thread is owner
139 return getExclusiveOwnerThread() == Thread.currentThread();
142 final ConditionObject newCondition() {
143 return new ConditionObject();
146 // Methods relayed from outer class
148 final Thread getOwner() {
149 return getState() == 0 ? null : getExclusiveOwnerThread();
152 final int getHoldCount() {
153 return isHeldExclusively() ? getState() : 0;
156 final boolean isLocked() {
157 return getState() != 0;
161 * Reconstitutes this lock instance from a stream.
162 * @param s the stream
164 private void readObject(java.io.ObjectInputStream s)
165 throws java.io.IOException, ClassNotFoundException {
166 s.defaultReadObject();
167 setState(0); // reset to unlocked state
172 * Sync object for non-fair locks
174 final static class NonfairSync extends Sync {
175 private static final long serialVersionUID = 7316153563782823691L;
178 * Performs lock. Try immediate barge, backing up to normal
179 * acquire on failure.
182 if (compareAndSetState(0, 1))
183 setExclusiveOwnerThread(Thread.currentThread());
188 protected final boolean tryAcquire(int acquires) {
189 return nonfairTryAcquire(acquires);
194 * Sync object for fair locks
196 final static class FairSync extends Sync {
197 private static final long serialVersionUID = -3000897897090466540L;
204 * Fair version of tryAcquire. Don't grant access unless
205 * recursive call or no waiters or is first.
207 protected final boolean tryAcquire(int acquires) {
208 final Thread current = Thread.currentThread();
211 if (isFirst(current) &&
212 compareAndSetState(0, acquires)) {
213 setExclusiveOwnerThread(current);
217 else if (current == getExclusiveOwnerThread()) {
218 int nextc = c + acquires;
220 throw new Error("Maximum lock count exceeded");
229 * Creates an instance of {@code ReentrantLock}.
230 * This is equivalent to using {@code ReentrantLock(false)}.
232 public ReentrantLock() {
233 sync = new NonfairSync();
237 * Creates an instance of {@code ReentrantLock} with the
238 * given fairness policy.
240 * @param fair {@code true} if this lock should use a fair ordering policy
242 public ReentrantLock(boolean fair) {
243 sync = (fair)? new FairSync() : new NonfairSync();
249 * <p>Acquires the lock if it is not held by another thread and returns
250 * immediately, setting the lock hold count to one.
252 * <p>If the current thread already holds the lock then the hold
253 * count is incremented by one and the method returns immediately.
255 * <p>If the lock is held by another thread then the
256 * current thread becomes disabled for thread scheduling
257 * purposes and lies dormant until the lock has been acquired,
258 * at which time the lock hold count is set to one.
265 * Acquires the lock unless the current thread is
266 * {@linkplain Thread#interrupt interrupted}.
268 * <p>Acquires the lock if it is not held by another thread and returns
269 * immediately, setting the lock hold count to one.
271 * <p>If the current thread already holds this lock then the hold count
272 * is incremented by one and the method returns immediately.
274 * <p>If the lock is held by another thread then the
275 * current thread becomes disabled for thread scheduling
276 * purposes and lies dormant until one of two things happens:
280 * <li>The lock is acquired by the current thread; or
282 * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
287 * <p>If the lock is acquired by the current thread then the lock hold
288 * count is set to one.
290 * <p>If the current thread:
294 * <li>has its interrupted status set on entry to this method; or
296 * <li>is {@linkplain Thread#interrupt interrupted} while acquiring
301 * then {@link InterruptedException} is thrown and the current thread's
302 * interrupted status is cleared.
304 * <p>In this implementation, as this method is an explicit
305 * interruption point, preference is given to responding to the
306 * interrupt over normal or reentrant acquisition of the lock.
308 * @throws InterruptedException if the current thread is interrupted
310 public void lockInterruptibly() throws InterruptedException {
311 sync.acquireInterruptibly(1);
315 * Acquires the lock only if it is not held by another thread at the time
318 * <p>Acquires the lock if it is not held by another thread and
319 * returns immediately with the value {@code true}, setting the
320 * lock hold count to one. Even when this lock has been set to use a
321 * fair ordering policy, a call to {@code tryLock()} <em>will</em>
322 * immediately acquire the lock if it is available, whether or not
323 * other threads are currently waiting for the lock.
324 * This "barging" behavior can be useful in certain
325 * circumstances, even though it breaks fairness. If you want to honor
326 * the fairness setting for this lock, then use
327 * {@link #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS) }
328 * which is almost equivalent (it also detects interruption).
330 * <p> If the current thread already holds this lock then the hold
331 * count is incremented by one and the method returns {@code true}.
333 * <p>If the lock is held by another thread then this method will return
334 * immediately with the value {@code false}.
336 * @return {@code true} if the lock was free and was acquired by the
337 * current thread, or the lock was already held by the current
338 * thread; and {@code false} otherwise
340 public boolean tryLock() {
341 return sync.nonfairTryAcquire(1);
345 * Acquires the lock if it is not held by another thread within the given
346 * waiting time and the current thread has not been
347 * {@linkplain Thread#interrupt interrupted}.
349 * <p>Acquires the lock if it is not held by another thread and returns
350 * immediately with the value {@code true}, setting the lock hold count
351 * to one. If this lock has been set to use a fair ordering policy then
352 * an available lock <em>will not</em> be acquired if any other threads
353 * are waiting for the lock. This is in contrast to the {@link #tryLock()}
354 * method. If you want a timed {@code tryLock} that does permit barging on
355 * a fair lock then combine the timed and un-timed forms together:
357 * <pre>if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
360 * <p>If the current thread
361 * already holds this lock then the hold count is incremented by one and
362 * the method returns {@code true}.
364 * <p>If the lock is held by another thread then the
365 * current thread becomes disabled for thread scheduling
366 * purposes and lies dormant until one of three things happens:
370 * <li>The lock is acquired by the current thread; or
372 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
373 * the current thread; or
375 * <li>The specified waiting time elapses
379 * <p>If the lock is acquired then the value {@code true} is returned and
380 * the lock hold count is set to one.
382 * <p>If the current thread:
386 * <li>has its interrupted status set on entry to this method; or
388 * <li>is {@linkplain Thread#interrupt interrupted} while
389 * acquiring the lock,
392 * then {@link InterruptedException} is thrown and the current thread's
393 * interrupted status is cleared.
395 * <p>If the specified waiting time elapses then the value {@code false}
396 * is returned. If the time is less than or equal to zero, the method
397 * will not wait at all.
399 * <p>In this implementation, as this method is an explicit
400 * interruption point, preference is given to responding to the
401 * interrupt over normal or reentrant acquisition of the lock, and
402 * over reporting the elapse of the waiting time.
404 * @param timeout the time to wait for the lock
405 * @param unit the time unit of the timeout argument
406 * @return {@code true} if the lock was free and was acquired by the
407 * current thread, or the lock was already held by the current
408 * thread; and {@code false} if the waiting time elapsed before
409 * the lock could be acquired
410 * @throws InterruptedException if the current thread is interrupted
411 * @throws NullPointerException if the time unit is null
414 public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
415 return sync.tryAcquireNanos(1, unit.toNanos(timeout));
419 * Attempts to release this lock.
421 * <p>If the current thread is the holder of this lock then the hold
422 * count is decremented. If the hold count is now zero then the lock
423 * is released. If the current thread is not the holder of this
424 * lock then {@link IllegalMonitorStateException} is thrown.
426 * @throws IllegalMonitorStateException if the current thread does not
429 public void unlock() {
434 * Returns a {@link Condition} instance for use with this
435 * {@link Lock} instance.
437 * <p>The returned {@link Condition} instance supports the same
438 * usages as do the {@link Object} monitor methods ({@link
439 * Object#wait() wait}, {@link Object#notify notify}, and {@link
440 * Object#notifyAll notifyAll}) when used with the built-in
445 * <li>If this lock is not held when any of the {@link Condition}
446 * {@linkplain Condition#await() waiting} or {@linkplain
447 * Condition#signal signalling} methods are called, then an {@link
448 * IllegalMonitorStateException} is thrown.
450 * <li>When the condition {@linkplain Condition#await() waiting}
451 * methods are called the lock is released and, before they
452 * return, the lock is reacquired and the lock hold count restored
453 * to what it was when the method was called.
455 * <li>If a thread is {@linkplain Thread#interrupt interrupted}
456 * while waiting then the wait will terminate, an {@link
457 * InterruptedException} will be thrown, and the thread's
458 * interrupted status will be cleared.
460 * <li> Waiting threads are signalled in FIFO order.
462 * <li>The ordering of lock reacquisition for threads returning
463 * from waiting methods is the same as for threads initially
464 * acquiring the lock, which is in the default case not specified,
465 * but for <em>fair</em> locks favors those threads that have been
466 * waiting the longest.
470 * @return the Condition object
472 public Condition newCondition() {
473 return sync.newCondition();
477 * Queries the number of holds on this lock by the current thread.
479 * <p>A thread has a hold on a lock for each lock action that is not
480 * matched by an unlock action.
482 * <p>The hold count information is typically only used for testing and
483 * debugging purposes. For example, if a certain section of code should
484 * not be entered with the lock already held then we can assert that
489 * ReentrantLock lock = new ReentrantLock();
492 * assert lock.getHoldCount() == 0;
503 * @return the number of holds on this lock by the current thread,
504 * or zero if this lock is not held by the current thread
506 public int getHoldCount() {
507 return sync.getHoldCount();
511 * Queries if this lock is held by the current thread.
513 * <p>Analogous to the {@link Thread#holdsLock} method for built-in
514 * monitor locks, this method is typically used for debugging and
515 * testing. For example, a method that should only be called while
516 * a lock is held can assert that this is the case:
520 * ReentrantLock lock = new ReentrantLock();
524 * assert lock.isHeldByCurrentThread();
530 * <p>It can also be used to ensure that a reentrant lock is used
531 * in a non-reentrant manner, for example:
535 * ReentrantLock lock = new ReentrantLock();
539 * assert !lock.isHeldByCurrentThread();
550 * @return {@code true} if current thread holds this lock and
551 * {@code false} otherwise
553 public boolean isHeldByCurrentThread() {
554 return sync.isHeldExclusively();
558 * Queries if this lock is held by any thread. This method is
559 * designed for use in monitoring of the system state,
560 * not for synchronization control.
562 * @return {@code true} if any thread holds this lock and
563 * {@code false} otherwise
565 public boolean isLocked() {
566 return sync.isLocked();
570 * Returns {@code true} if this lock has fairness set true.
572 * @return {@code true} if this lock has fairness set true
574 public final boolean isFair() {
575 return sync instanceof FairSync;
579 * Returns the thread that currently owns this lock, or
580 * {@code null} if not owned. When this method is called by a
581 * thread that is not the owner, the return value reflects a
582 * best-effort approximation of current lock status. For example,
583 * the owner may be momentarily {@code null} even if there are
584 * threads trying to acquire the lock but have not yet done so.
585 * This method is designed to facilitate construction of
586 * subclasses that provide more extensive lock monitoring
589 * @return the owner, or {@code null} if not owned
591 protected Thread getOwner() {
592 return sync.getOwner();
596 * Queries whether any threads are waiting to acquire this lock. Note that
597 * because cancellations may occur at any time, a {@code true}
598 * return does not guarantee that any other thread will ever
599 * acquire this lock. This method is designed primarily for use in
600 * monitoring of the system state.
602 * @return {@code true} if there may be other threads waiting to
605 public final boolean hasQueuedThreads() {
606 return sync.hasQueuedThreads();
611 * Queries whether the given thread is waiting to acquire this
612 * lock. Note that because cancellations may occur at any time, a
613 * {@code true} return does not guarantee that this thread
614 * will ever acquire this lock. This method is designed primarily for use
615 * in monitoring of the system state.
617 * @param thread the thread
618 * @return {@code true} if the given thread is queued waiting for this lock
619 * @throws NullPointerException if the thread is null
621 public final boolean hasQueuedThread(Thread thread) {
622 return sync.isQueued(thread);
627 * Returns an estimate of the number of threads waiting to
628 * acquire this lock. The value is only an estimate because the number of
629 * threads may change dynamically while this method traverses
630 * internal data structures. This method is designed for use in
631 * monitoring of the system state, not for synchronization
634 * @return the estimated number of threads waiting for this lock
636 public final int getQueueLength() {
637 return sync.getQueueLength();
641 * Returns a collection containing threads that may be waiting to
642 * acquire this lock. Because the actual set of threads may change
643 * dynamically while constructing this result, the returned
644 * collection is only a best-effort estimate. The elements of the
645 * returned collection are in no particular order. This method is
646 * designed to facilitate construction of subclasses that provide
647 * more extensive monitoring facilities.
649 * @return the collection of threads
651 protected Collection<Thread> getQueuedThreads() {
652 return sync.getQueuedThreads();
656 * Queries whether any threads are waiting on the given condition
657 * associated with this lock. Note that because timeouts and
658 * interrupts may occur at any time, a {@code true} return does
659 * not guarantee that a future {@code signal} will awaken any
660 * threads. This method is designed primarily for use in
661 * monitoring of the system state.
663 * @param condition the condition
664 * @return {@code true} if there are any waiting threads
665 * @throws IllegalMonitorStateException if this lock is not held
666 * @throws IllegalArgumentException if the given condition is
667 * not associated with this lock
668 * @throws NullPointerException if the condition is null
670 public boolean hasWaiters(Condition condition) {
671 if (condition == null)
672 throw new NullPointerException();
673 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
674 throw new IllegalArgumentException("not owner");
675 return sync.hasWaiters((AbstractQueuedSynchronizer.ConditionObject)condition);
679 * Returns an estimate of the number of threads waiting on the
680 * given condition associated with this lock. Note that because
681 * timeouts and interrupts may occur at any time, the estimate
682 * serves only as an upper bound on the actual number of waiters.
683 * This method is designed for use in monitoring of the system
684 * state, not for synchronization control.
686 * @param condition the condition
687 * @return the estimated number of waiting threads
688 * @throws IllegalMonitorStateException if this lock is not held
689 * @throws IllegalArgumentException if the given condition is
690 * not associated with this lock
691 * @throws NullPointerException if the condition is null
693 public int getWaitQueueLength(Condition condition) {
694 if (condition == null)
695 throw new NullPointerException();
696 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
697 throw new IllegalArgumentException("not owner");
698 return sync.getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject)condition);
702 * Returns a collection containing those threads that may be
703 * waiting on the given condition associated with this lock.
704 * Because the actual set of threads may change dynamically while
705 * constructing this result, the returned collection is only a
706 * best-effort estimate. The elements of the returned collection
707 * are in no particular order. This method is designed to
708 * facilitate construction of subclasses that provide more
709 * extensive condition monitoring facilities.
711 * @param condition the condition
712 * @return the collection of threads
713 * @throws IllegalMonitorStateException if this lock is not held
714 * @throws IllegalArgumentException if the given condition is
715 * not associated with this lock
716 * @throws NullPointerException if the condition is null
718 protected Collection<Thread> getWaitingThreads(Condition condition) {
719 if (condition == null)
720 throw new NullPointerException();
721 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
722 throw new IllegalArgumentException("not owner");
723 return sync.getWaitingThreads((AbstractQueuedSynchronizer.ConditionObject)condition);
727 * Returns a string identifying this lock, as well as its lock state.
728 * The state, in brackets, includes either the String {@code "Unlocked"}
729 * or the String {@code "Locked by"} followed by the
730 * {@linkplain Thread#getName name} of the owning thread.
732 * @return a string identifying this lock, as well as its lock state
734 public String toString() {
735 Thread o = sync.getOwner();
736 return super.toString() + ((o == null) ?
738 "[Locked by thread " + o.getName() + "]");