KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > internal > concurrent > ReentrantLock


1 /*
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
5  */

6
7 package org.apache.beehive.netui.util.internal.concurrent;
8
9 import java.util.*;
10
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  * @author Dawid Kurzyniec
78  */

79 class ReentrantLock implements Lock, java.io.Serializable JavaDoc,
80                                       CondVar.LockInfo {
81     private static final long serialVersionUID = 7373984872572414699L;
82
83     private final Impl impl;
84
85     static abstract class Impl {
86         protected Thread JavaDoc owner_ = null;
87         protected int holds_ = 0;
88
89         protected Impl() {}
90
91         public abstract void lockInterruptibly() throws InterruptedException JavaDoc;
92
93         public boolean tryLock() {
94             Thread JavaDoc caller = Thread.currentThread();
95             synchronized (this) {
96                 if (owner_ == null) {
97                     owner_ = caller;
98                     holds_ = 1;
99                     return true;
100                 }
101                 else if (caller == owner_) {
102                     ++holds_;
103                     return true;
104                 }
105             }
106             return false;
107         }
108
109         public abstract boolean tryLock(long nanos) throws InterruptedException JavaDoc;
110
111         public abstract void unlock();
112
113         public synchronized int getHoldCount() {
114             return isHeldByCurrentThread() ? holds_ : 0;
115         }
116
117         public synchronized boolean isHeldByCurrentThread() {
118             return Thread.currentThread() == owner_;
119         }
120
121         public synchronized boolean isLocked() {
122             return owner_ != null;
123         }
124
125         public abstract boolean isFair();
126
127         protected synchronized Thread JavaDoc getOwner() {
128             return owner_;
129         }
130
131         public boolean hasQueuedThreads() {
132             throw new UnsupportedOperationException JavaDoc("Use FAIR version");
133         }
134
135         public int getQueueLength() {
136             throw new UnsupportedOperationException JavaDoc("Use FAIR version");
137         }
138
139         public Collection getQueuedThreads() {
140             throw new UnsupportedOperationException JavaDoc("Use FAIR version");
141         }
142
143         public boolean isQueued(Thread JavaDoc thread) {
144             throw new UnsupportedOperationException JavaDoc("Use FAIR version");
145         }
146     }
147
148     final static class NonfairImpl extends Impl implements java.io.Serializable JavaDoc {
149
150         NonfairImpl() {}
151
152         public void lockInterruptibly() throws InterruptedException JavaDoc {
153             if (Thread.interrupted()) throw new InterruptedException JavaDoc();
154             Thread JavaDoc caller = Thread.currentThread();
155             synchronized (this) {
156                 if (owner_ == null) {
157                     owner_ = caller;
158                     holds_ = 1;
159                     return;
160                 }
161                 else if (caller == owner_) {
162                     ++holds_;
163                     return;
164                 }
165                 else {
166                     try {
167                         do { wait(); } while (owner_ != null);
168                         owner_ = caller;
169                         holds_ = 1;
170                         return;
171                     }
172                     catch (InterruptedException JavaDoc ex) {
173                         notify();
174                         throw ex;
175                     }
176                 }
177             }
178         }
179
180         public boolean tryLock(long nanos) throws InterruptedException JavaDoc {
181             if (Thread.interrupted()) throw new InterruptedException JavaDoc();
182             Thread JavaDoc caller = Thread.currentThread();
183
184             synchronized (this) {
185                 if (owner_ == null) {
186                     owner_ = caller;
187                     holds_ = 1;
188                     return true;
189                 }
190                 else if (caller == owner_) {
191                     ++holds_;
192                     return true;
193                 }
194                 else if (nanos <= 0)
195                     return false;
196                 else {
197                     long deadline = Utils.nanoTime() + nanos;
198                     try {
199                         for (; ; ) {
200                             TimeUnit.NANOSECONDS.timedWait(this, nanos);
201                             if (caller == owner_) {
202                                 ++holds_;
203                                 return true;
204                             }
205                             else if (owner_ == null) {
206                                 owner_ = caller;
207                                 holds_ = 1;
208                                 return true;
209                             }
210                             else {
211                                 nanos = deadline - Utils.nanoTime();
212                                 if (nanos <= 0)
213                                     return false;
214                             }
215                         }
216                     }
217                     catch (InterruptedException JavaDoc ex) {
218                         notify();
219                         throw ex;
220                     }
221                 }
222             }
223         }
224
225         public synchronized void unlock() {
226             if (Thread.currentThread() != owner_)
227                 throw new IllegalMonitorStateException JavaDoc("Not owner");
228
229             if (--holds_ == 0) {
230                 owner_ = null;
231                 notify();
232             }
233         }
234
235         public final boolean isFair() {
236             return false;
237         }
238     }
239
240     final static class FairImpl extends Impl implements WaitQueue.QueuedSync,
241                                                         java.io.Serializable JavaDoc {
242
243         private final WaitQueue wq_ = new FIFOWaitQueue();
244
245         FairImpl() {}
246
247         public synchronized boolean recheck(WaitQueue.WaitNode node) {
248             Thread JavaDoc caller = Thread.currentThread();
249             if (owner_ == null) {
250                 owner_ = caller;
251                 holds_ = 1;
252                 return true;
253             }
254             else if (caller == owner_) {
255                 ++holds_;
256                 return true;
257             }
258             wq_.insert(node);
259             return false;
260         }
261
262         public synchronized void takeOver(WaitQueue.WaitNode node) {
263             // assert (holds_ == 1 && owner_ == Thread.currentThread()
264
owner_ = node.getOwner();
265         }
266
267         public void lockInterruptibly() throws InterruptedException JavaDoc {
268             if (Thread.interrupted()) throw new InterruptedException JavaDoc();
269             Thread JavaDoc caller = Thread.currentThread();
270             synchronized (this) {
271                 if (owner_ == null) {
272                     owner_ = caller;
273                     holds_ = 1;
274                     return;
275                 }
276                 else if (caller == owner_) {
277                     ++holds_;
278                     return;
279                 }
280             }
281             WaitQueue.WaitNode n = new WaitQueue.WaitNode();
282             n.doWait(this);
283         }
284
285         public boolean tryLock(long nanos) throws InterruptedException JavaDoc {
286             if (Thread.interrupted()) throw new InterruptedException JavaDoc();
287             Thread JavaDoc caller = Thread.currentThread();
288             synchronized (this) {
289                 if (owner_ == null) {
290                     owner_ = caller;
291                     holds_ = 1;
292                     return true;
293                 }
294                 else if (caller == owner_) {
295                     ++holds_;
296                     return true;
297                 }
298             }
299             WaitQueue.WaitNode n = new WaitQueue.WaitNode();
300             return n.doTimedWait(this, nanos);
301         }
302
303         protected synchronized WaitQueue.WaitNode getSignallee(Thread JavaDoc caller) {
304             if (caller != owner_)
305                 throw new IllegalMonitorStateException JavaDoc("Not owner");
306             // assert (holds_ > 0)
307
if (holds_ >= 2) { // current thread will keep the lock
308
--holds_;
309                 return null;
310             }
311             // assert (holds_ == 1)
312
WaitQueue.WaitNode w = wq_.extract();
313             if (w == null) { // if none, clear for new arrivals
314
owner_ = null;
315                 holds_ = 0;
316             }
317             return w;
318         }
319
320         public void unlock() {
321             Thread JavaDoc caller = Thread.currentThread();
322             for (;;) {
323                 WaitQueue.WaitNode w = getSignallee(caller);
324                 if (w == null) return; // no one to signal
325
if (w.signal(this)) return; // notify if still waiting, else skip
326
}
327         }
328
329         public final boolean isFair() {
330             return true;
331         }
332
333         public synchronized boolean hasQueuedThreads() {
334             return wq_.hasNodes();
335         }
336
337         public synchronized int getQueueLength() {
338             return wq_.getLength();
339         }
340
341         public synchronized Collection getQueuedThreads() {
342             return wq_.getWaitingThreads();
343         }
344
345         public synchronized boolean isQueued(Thread JavaDoc thread) {
346             return wq_.isWaiting(thread);
347         }
348     }
349
350     /**
351      * Creates an instance of <tt>ReentrantLock</tt>.
352      * This is equivalent to using <tt>ReentrantLock(false)</tt>.
353      */

354     public ReentrantLock() {
355         impl = new NonfairImpl();
356     }
357
358     /**
359      * Creates an instance of <tt>ReentrantLock</tt> with the
360      * given fairness policy.
361      * @param fair true if this lock will be fair; else false
362      */

363     public ReentrantLock(boolean fair) {
364         impl = (fair)? (Impl)new FairImpl() : new NonfairImpl();
365     }
366
367
368     /**
369      * Acquires the lock.
370      *
371      * <p>Acquires the lock if it is not held by another thread and returns
372      * immediately, setting the lock hold count to one.
373      *
374      * <p>If the current thread
375      * already holds the lock then the hold count is incremented by one and
376      * the method returns immediately.
377      *
378      * <p>If the lock is held by another thread then the
379      * current thread becomes disabled for thread scheduling
380      * purposes and lies dormant until the lock has been acquired,
381      * at which time the lock hold count is set to one.
382      */

383     public void lock() {
384         boolean wasInterrupted = false;
385         while (true) {
386             try {
387                 impl.lockInterruptibly();
388                 if (wasInterrupted) {
389                     Thread.currentThread().interrupt();
390                 }
391                 return;
392             }
393             catch (InterruptedException JavaDoc e) {
394                 wasInterrupted = true;
395             }
396         }
397     }
398
399     /**
400      * Acquires the lock unless the current thread is
401      * {@link Thread#interrupt interrupted}.
402      *
403      * <p>Acquires the lock if it is not held by another thread and returns
404      * immediately, setting the lock hold count to one.
405      *
406      * <p>If the current thread already holds this lock then the hold count
407      * is incremented by one and the method returns immediately.
408      *
409      * <p>If the lock is held by another thread then the
410      * current thread becomes disabled for thread scheduling
411      * purposes and lies dormant until one of two things happens:
412      *
413      * <ul>
414      *
415      * <li>The lock is acquired by the current thread; or
416      *
417      * <li>Some other thread {@link Thread#interrupt interrupts} the current
418      * thread.
419      *
420      * </ul>
421      *
422      * <p>If the lock is acquired by the current thread then the lock hold
423      * count is set to one.
424      *
425      * <p>If the current thread:
426      *
427      * <ul>
428      *
429      * <li>has its interrupted status set on entry to this method; or
430      *
431      * <li>is {@link Thread#interrupt interrupted} while acquiring
432      * the lock,
433      *
434      * </ul>
435      *
436      * then {@link InterruptedException} is thrown and the current thread's
437      * interrupted status is cleared.
438      *
439      * <p>In this implementation, as this method is an explicit interruption
440      * point, preference is
441      * given to responding to the interrupt over normal or reentrant
442      * acquisition of the lock.
443      *
444      * @throws InterruptedException if the current thread is interrupted
445      */

446     public void lockInterruptibly() throws InterruptedException JavaDoc {
447         impl.lockInterruptibly();
448     }
449
450     /**
451      * Acquires the lock only if it is not held by another thread at the time
452      * of invocation.
453      *
454      * <p>Acquires the lock if it is not held by another thread and
455      * returns immediately with the value <tt>true</tt>, setting the
456      * lock hold count to one. Even when this lock has been set to use a
457      * fair ordering policy, a call to <tt>tryLock()</tt> <em>will</em>
458      * immediately acquire the lock if it is available, whether or not
459      * other threads are currently waiting for the lock.
460      * This &quot;barging&quot; behavior can be useful in certain
461      * circumstances, even though it breaks fairness. If you want to honor
462      * the fairness setting for this lock, then use
463      * {@link #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS) }
464      * which is almost equivalent (it also detects interruption).
465      *
466      * <p> If the current thread
467      * already holds this lock then the hold count is incremented by one and
468      * the method returns <tt>true</tt>.
469      *
470      * <p>If the lock is held by another thread then this method will return
471      * immediately with the value <tt>false</tt>.
472      *
473      * @return <tt>true</tt> if the lock was free and was acquired by the
474      * current thread, or the lock was already held by the current thread; and
475      * <tt>false</tt> otherwise.
476      */

477     public boolean tryLock() {
478         return impl.tryLock();
479     }
480
481     /**
482      * Acquires the lock if it is not held by another thread within the given
483      * waiting time and the current thread has not been
484      * {@link Thread#interrupt interrupted}.
485      *
486      * <p>Acquires the lock if it is not held by another thread and returns
487      * immediately with the value <tt>true</tt>, setting the lock hold count
488      * to one. If this lock has been set to use a fair ordering policy then
489      * an available lock <em>will not</em> be acquired if any other threads
490      * are waiting for the lock. This is in contrast to the {@link #tryLock()}
491      * method. If you want a timed <tt>tryLock</tt> that does permit barging on
492      * a fair lock then combine the timed and un-timed forms together:
493      *
494      * <pre>if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
495      * </pre>
496      *
497      * <p>If the current thread
498      * already holds this lock then the hold count is incremented by one and
499      * the method returns <tt>true</tt>.
500      *
501      * <p>If the lock is held by another thread then the
502      * current thread becomes disabled for thread scheduling
503      * purposes and lies dormant until one of three things happens:
504      *
505      * <ul>
506      *
507      * <li>The lock is acquired by the current thread; or
508      *
509      * <li>Some other thread {@link Thread#interrupt interrupts} the current
510      * thread; or
511      *
512      * <li>The specified waiting time elapses
513      *
514      * </ul>
515      *
516      * <p>If the lock is acquired then the value <tt>true</tt> is returned and
517      * the lock hold count is set to one.
518      *
519      * <p>If the current thread:
520      *
521      * <ul>
522      *
523      * <li>has its interrupted status set on entry to this method; or
524      *
525      * <li>is {@link Thread#interrupt interrupted} while acquiring
526      * the lock,
527      *
528      * </ul>
529      * then {@link InterruptedException} is thrown and the current thread's
530      * interrupted status is cleared.
531      *
532      * <p>If the specified waiting time elapses then the value <tt>false</tt>
533      * is returned.
534      * If the time is
535      * less than or equal to zero, the method will not wait at all.
536      *
537      * <p>In this implementation, as this method is an explicit interruption
538      * point, preference is
539      * given to responding to the interrupt over normal or reentrant
540      * acquisition of the lock, and over reporting the elapse of the waiting
541      * time.
542      *
543      * @param timeout the time to wait for the lock
544      * @param unit the time unit of the timeout argument
545      *
546      * @return <tt>true</tt> if the lock was free and was acquired by the
547      * current thread, or the lock was already held by the current thread; and
548      * <tt>false</tt> if the waiting time elapsed before the lock could be
549      * acquired.
550      *
551      * @throws InterruptedException if the current thread is interrupted
552      * @throws NullPointerException if unit is null
553      *
554      */

555     public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException JavaDoc {
556         return impl.tryLock(unit.toNanos(timeout));
557     }
558
559     /**
560      * Attempts to release this lock.
561      *
562      * <p>If the current thread is the
563      * holder of this lock then the hold count is decremented. If the
564      * hold count is now zero then the lock is released. If the
565      * current thread is not the holder of this lock then {@link
566      * IllegalMonitorStateException} is thrown.
567      * @throws IllegalMonitorStateException if the current thread does not
568      * hold this lock.
569      */

570     public void unlock() {
571         impl.unlock();
572     }
573
574     /**
575      * Returns a {@link Condition} instance for use with this
576      * {@link Lock} instance.
577      *
578      * <p>The returned {@link Condition} instance supports the same
579      * usages as do the {@link Object} monitor methods ({@link
580      * Object#wait() wait}, {@link Object#notify notify}, and {@link
581      * Object#notifyAll notifyAll}) when used with the built-in
582      * monitor lock.
583      *
584      * <ul>
585      *
586      * <li>If this lock is not held when any of the {@link Condition}
587      * {@link Condition#await() waiting} or {@link Condition#signal
588      * signalling} methods are called, then an {@link
589      * IllegalMonitorStateException} is thrown.
590      *
591      * <li>When the condition {@link Condition#await() waiting}
592      * methods are called the lock is released and, before they
593      * return, the lock is reacquired and the lock hold count restored
594      * to what it was when the method was called.
595      *
596      * <li>If a thread is {@link Thread#interrupt interrupted} while
597      * waiting then the wait will terminate, an {@link
598      * InterruptedException} will be thrown, and the thread's
599      * interrupted status will be cleared.
600      *
601      * <li> Waiting threads are signalled in FIFO order
602      *
603      * <li>The ordering of lock reacquisition for threads returning
604      * from waiting methods is the same as for threads initially
605      * acquiring the lock, which is in the default case not specified,
606      * but for <em>fair</em> locks favors those threads that have been
607      * waiting the longest.
608      *
609      * </ul>
610      *
611      * @return the Condition object
612      */

613     public Condition newCondition() {
614         return new CondVar(this);
615     }
616
617     /**
618      * Queries the number of holds on this lock by the current thread.
619      *
620      * <p>A thread has a hold on a lock for each lock action that is not
621      * matched by an unlock action.
622      *
623      * <p>The hold count information is typically only used for testing and
624      * debugging purposes. For example, if a certain section of code should
625      * not be entered with the lock already held then we can assert that
626      * fact:
627      *
628      * <pre>
629      * class X {
630      * ReentrantLock lock = new ReentrantLock();
631      * // ...
632      * public void m() {
633      * assert lock.getHoldCount() == 0;
634      * lock.lock();
635      * try {
636      * // ... method body
637      * } finally {
638      * lock.unlock();
639      * }
640      * }
641      * }
642      * </pre>
643      *
644      * @return the number of holds on this lock by the current thread,
645      * or zero if this lock is not held by the current thread.
646      */

647     public int getHoldCount() {
648         return impl.getHoldCount();
649     }
650
651     /**
652      * Queries if this lock is held by the current thread.
653      *
654      * <p>Analogous to the {@link Thread#holdsLock} method for built-in
655      * monitor locks, this method is typically used for debugging and
656      * testing. For example, a method that should only be called while
657      * a lock is held can assert that this is the case:
658      *
659      * <pre>
660      * class X {
661      * ReentrantLock lock = new ReentrantLock();
662      * // ...
663      *
664      * public void m() {
665      * assert lock.isHeldByCurrentThread();
666      * // ... method body
667      * }
668      * }
669      * </pre>
670      *
671      * <p>It can also be used to ensure that a reentrant lock is used
672      * in a non-reentrant manner, for example:
673      *
674      * <pre>
675      * class X {
676      * ReentrantLock lock = new ReentrantLock();
677      * // ...
678      *
679      * public void m() {
680      * assert !lock.isHeldByCurrentThread();
681      * lock.lock();
682      * try {
683      * // ... method body
684      * } finally {
685      * lock.unlock();
686      * }
687      * }
688      * }
689      * </pre>
690      * @return <tt>true</tt> if current thread holds this lock and
691      * <tt>false</tt> otherwise.
692      */

693     public boolean isHeldByCurrentThread() {
694         return impl.isHeldByCurrentThread();
695     }
696
697     /**
698      * Queries if this lock is held by any thread. This method is
699      * designed for use in monitoring of the system state,
700      * not for synchronization control.
701      * @return <tt>true</tt> if any thread holds this lock and
702      * <tt>false</tt> otherwise.
703      */

704     public boolean isLocked() {
705         return impl.isLocked();
706     }
707
708     /**
709      * Returns true if this lock has fairness set true.
710      * @return true if this lock has fairness set true.
711      */

712     public final boolean isFair() {
713         return impl.isFair();
714     }
715
716     /**
717      * Returns the thread that currently owns this lock, or
718      * <tt>null</tt> if not owned. Note that the owner may be
719      * momentarily <tt>null</tt> even if there are threads trying to
720      * acquire the lock but have not yet done so. This method is
721      * designed to facilitate construction of subclasses that provide
722      * more extensive lock monitoring facilities.
723      * @return the owner, or <tt>null</tt> if not owned.
724      */

725     protected Thread JavaDoc getOwner() {
726         return impl.getOwner();
727     }
728
729     /**
730      * Queries whether any threads are waiting to acquire this lock. Note that
731      * because cancellations may occur at any time, a <tt>true</tt>
732      * return does not guarantee that any other thread will ever
733      * acquire this lock. This method is designed primarily for use in
734      * monitoring of the system state.
735      *
736      * @return true if there may be other threads waiting to acquire
737      * the lock.
738      */

739     public final boolean hasQueuedThreads() {
740         return impl.hasQueuedThreads();
741     }
742
743
744     /**
745      * Queries whether the given thread is waiting to acquire this
746      * lock. Note that because cancellations may occur at any time, a
747      * <tt>true</tt> return does not guarantee that this thread
748      * will ever acquire this lock. This method is designed primarily for use
749      * in monitoring of the system state.
750      *
751      * @param thread the thread
752      * @return true if the given thread is queued waiting for this lock.
753      * @throws NullPointerException if thread is null
754      */

755     public final boolean hasQueuedThread(Thread JavaDoc thread) {
756         return impl.isQueued(thread);
757     }
758
759
760     /**
761      * Returns an estimate of the number of threads waiting to
762      * acquire this lock. The value is only an estimate because the number of
763      * threads may change dynamically while this method traverses
764      * internal data structures. This method is designed for use in
765      * monitoring of the system state, not for synchronization
766      * control.
767      * @return the estimated number of threads waiting for this lock
768      */

769     public final int getQueueLength() {
770         return impl.getQueueLength();
771     }
772
773     /**
774      * Returns a collection containing threads that may be waiting to
775      * acquire this lock. Because the actual set of threads may change
776      * dynamically while constructing this result, the returned
777      * collection is only a best-effort estimate. The elements of the
778      * returned collection are in no particular order. This method is
779      * designed to facilitate construction of subclasses that provide
780      * more extensive monitoring facilities.
781      * @return the collection of threads
782      */

783     protected Collection getQueuedThreads() {
784         return impl.getQueuedThreads();
785     }
786
787 // /**
788
// * Queries whether any threads are waiting on the given condition
789
// * associated with this lock. Note that because timeouts and
790
// * interrupts may occur at any time, a <tt>true</tt> return does
791
// * not guarantee that a future <tt>signal</tt> will awaken any
792
// * threads. This method is designed primarily for use in
793
// * monitoring of the system state.
794
// * @param condition the condition
795
// * @return <tt>true</tt> if there are any waiting threads.
796
// * @throws IllegalMonitorStateException if this lock
797
// * is not held
798
// * @throws IllegalArgumentException if the given condition is
799
// * not associated with this lock
800
// * @throws NullPointerException if condition null
801
// */
802
// public boolean hasWaiters(Condition condition) {
803
// if (condition == null)
804
// throw new NullPointerException();
805
// if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
806
// throw new IllegalArgumentException("not owner");
807
// return sync.hasWaiters((AbstractQueuedSynchronizer.ConditionObject)condition);
808
// }
809

810 // /**
811
// * Returns an estimate of the number of threads waiting on the
812
// * given condition associated with this lock. Note that because
813
// * timeouts and interrupts may occur at any time, the estimate
814
// * serves only as an upper bound on the actual number of waiters.
815
// * This method is designed for use in monitoring of the system
816
// * state, not for synchronization control.
817
// * @param condition the condition
818
// * @return the estimated number of waiting threads.
819
// * @throws IllegalMonitorStateException if this lock
820
// * is not held
821
// * @throws IllegalArgumentException if the given condition is
822
// * not associated with this lock
823
// * @throws NullPointerException if condition null
824
// */
825
// public int getWaitQueueLength(Condition condition) {
826
// if (condition == null)
827
// throw new NullPointerException();
828
// if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
829
// throw new IllegalArgumentException("not owner");
830
// return sync.getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject)condition);
831
// }
832

833 // /**
834
// * Returns a collection containing those threads that may be
835
// * waiting on the given condition associated with this lock.
836
// * Because the actual set of threads may change dynamically while
837
// * constructing this result, the returned collection is only a
838
// * best-effort estimate. The elements of the returned collection
839
// * are in no particular order. This method is designed to
840
// * facilitate construction of subclasses that provide more
841
// * extensive condition monitoring facilities.
842
// * @param condition the condition
843
// * @return the collection of threads
844
// * @throws IllegalMonitorStateException if this lock
845
// * is not held
846
// * @throws IllegalArgumentException if the given condition is
847
// * not associated with this lock
848
// * @throws NullPointerException if condition null
849
// */
850
// protected Collection getWaitingThreads(Condition condition) {
851
// if (condition == null)
852
// throw new NullPointerException();
853
// if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
854
// throw new IllegalArgumentException("not owner");
855
// return sync.getWaitingThreads((AbstractQueuedSynchronizer.ConditionObject)condition);
856
// }
857

858     /**
859      * Returns a string identifying this lock, as well as its lock
860      * state. The state, in brackets, includes either the String
861      * &quot;Unlocked&quot; or the String &quot;Locked by&quot;
862      * followed by the {@link Thread#getName} of the owning thread.
863      * @return a string identifying this lock, as well as its lock state.
864      */

865     public String JavaDoc toString() {
866         Thread JavaDoc owner = getOwner();
867         return super.toString() + ((owner == null) ?
868                                    "[Unlocked]" :
869                                    "[Locked by thread " + owner.getName() + "]");
870     }
871 }
872
Popular Tags