ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Semaphore.java
Revision: 1.42
Committed: Tue Feb 10 12:16:08 2004 UTC (20 years, 3 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_PFD
Changes since 1.41: +1 -1 lines
Log Message:
Wording fixes and improvements

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.24 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 dl 1.2 */
6    
7 tim 1.1 package java.util.concurrent;
8 dl 1.16 import java.util.*;
9 dl 1.5 import java.util.concurrent.locks.*;
10 dl 1.16 import java.util.concurrent.atomic.*;
11 tim 1.1
12     /**
13     * A counting semaphore. Conceptually, a semaphore maintains a set of
14     * permits. Each {@link #acquire} blocks if necessary until a permit is
15     * available, and then takes it. Each {@link #release} adds a permit,
16     * potentially releasing a blocking acquirer.
17     * However, no actual permit objects are used; the <tt>Semaphore</tt> just
18     * keeps a count of the number available and acts accordingly.
19     *
20 dl 1.16 * <p>Semaphores are often used to restrict the number of threads than can
21 tim 1.1 * access some (physical or logical) resource. For example, here is
22     * a class that uses a semaphore to control access to a pool of items:
23     * <pre>
24     * class Pool {
25     * private static final MAX_AVAILABLE = 100;
26 dl 1.16 * private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);
27 tim 1.1 *
28     * public Object getItem() throws InterruptedException {
29     * available.acquire();
30     * return getNextAvailableItem();
31     * }
32     *
33     * public void putItem(Object x) {
34     * if (markAsUnused(x))
35     * available.release();
36     * }
37     *
38     * // Not a particularly efficient data structure; just for demo
39     *
40     * protected Object[] items = ... whatever kinds of items being managed
41     * protected boolean[] used = new boolean[MAX_AVAILABLE];
42     *
43     * protected synchronized Object getNextAvailableItem() {
44     * for (int i = 0; i < MAX_AVAILABLE; ++i) {
45     * if (!used[i]) {
46     * used[i] = true;
47     * return items[i];
48     * }
49     * }
50     * return null; // not reached
51     * }
52     *
53     * protected synchronized boolean markAsUnused(Object item) {
54     * for (int i = 0; i < MAX_AVAILABLE; ++i) {
55     * if (item == items[i]) {
56     * if (used[i]) {
57     * used[i] = false;
58     * return true;
59 tim 1.8 * } else
60 tim 1.1 * return false;
61     * }
62     * }
63     * return false;
64     * }
65     *
66     * }
67     * </pre>
68     *
69 dl 1.16 * <p>Before obtaining an item each thread must acquire a permit from
70     * the semaphore, guaranteeing that an item is available for use. When
71     * the thread has finished with the item it is returned back to the
72     * pool and a permit is returned to the semaphore, allowing another
73     * thread to acquire that item. Note that no synchronization lock is
74     * held when {@link #acquire} is called as that would prevent an item
75     * from being returned to the pool. The semaphore encapsulates the
76     * synchronization needed to restrict access to the pool, separately
77     * from any synchronization needed to maintain the consistency of the
78     * pool itself.
79     *
80     * <p>A semaphore initialized to one, and which is used such that it
81     * only has at most one permit available, can serve as a mutual
82     * exclusion lock. This is more commonly known as a <em>binary
83     * semaphore</em>, because it only has two states: one permit
84     * available, or zero permits available. When used in this way, the
85     * binary semaphore has the property (unlike many {@link Lock}
86 dl 1.18 * implementations), that the &quot;lock&quot; can be released by a
87 dl 1.16 * thread other than the owner (as semaphores have no notion of
88     * ownership). This can be useful in some specialized contexts, such
89     * as deadlock recovery.
90     *
91 dl 1.36 * <p> The constructor for this class optionally accepts a
92     * <em>fairness</em> parameter. When set false, this class makes no
93     * guarantees about the order in which threads acquire permits. In
94     * particular, <em>barging</em> is permitted, that is, a thread
95     * invoking {@link #acquire} can be allocated a permit ahead of a
96     * thread that has been waiting. When fairness is set true, the
97     * semaphore guarantees that threads invoking any of the {@link
98     * #acquire() acquire} methods are allocated permits in the order in
99     * which their invocation of those methods was processed
100     * (first-in-first-out; FIFO). Note that FIFO ordering necessarily
101     * applies to specific internal points of execution within these
102     * methods. So, it is possible for one thread to invoke
103     * <tt>acquire</tt> before another, but reach the ordering point after
104     * the other, and similarly upon return from the method.
105 dl 1.16 *
106     * <p>Generally, semaphores used to control resource access should be
107     * initialized as fair, to ensure that no thread is starved out from
108     * accessing a resource. When using semaphores for other kinds of
109     * synchronization control, the throughput advantages of non-fair
110     * ordering often outweigh fairness considerations.
111     *
112     * <p>This class also provides convenience methods to {@link
113     * #acquire(int) acquire} and {@link #release(int) release} multiple
114     * permits at a time. Beware of the increased risk of indefinite
115 dl 1.19 * postponement when these methods are used without fairness set true.
116 tim 1.1 *
117     * @since 1.5
118 dl 1.4 * @author Doug Lea
119 tim 1.1 *
120     */
121 dl 1.16
122 dl 1.23 public class Semaphore implements java.io.Serializable {
123 dl 1.16 private static final long serialVersionUID = -3222578661600680210L;
124 dl 1.25 /** All mechanics via AbstractQueuedSynchronizer subclass */
125 dl 1.23 private final Sync sync;
126    
127 dl 1.25 /**
128 dl 1.34 * Synchronization implementation for semaphore. Uses AQS state
129     * to represent permits. Subclassed into fair and nonfair
130     * versions.
131 dl 1.25 */
132 dl 1.34 abstract static class Sync extends AbstractQueuedSynchronizer {
133     Sync(int permits) {
134 dl 1.29 setState(permits);
135 dl 1.23 }
136    
137 dl 1.34 final int getPermits() {
138 dl 1.29 return getState();
139     }
140    
141 dl 1.34 final int nonfairTryAcquireShared(int acquires) {
142 dl 1.23 for (;;) {
143 dl 1.29 int available = getState();
144 dl 1.23 int remaining = available - acquires;
145     if (remaining < 0 ||
146 dl 1.29 compareAndSetState(available, remaining))
147 dl 1.23 return remaining;
148     }
149     }
150    
151 dl 1.34 protected final boolean tryReleaseShared(int releases) {
152 dl 1.23 for (;;) {
153 dl 1.29 int p = getState();
154     if (compareAndSetState(p, p + releases))
155 dl 1.23 return true;
156     }
157     }
158 dl 1.29
159 dl 1.34 final void reducePermits(int reductions) {
160 dl 1.29 for (;;) {
161     int current = getState();
162     int next = current - reductions;
163     if (compareAndSetState(current, next))
164     return;
165     }
166     }
167 dl 1.31
168 dl 1.34 final int drainPermits() {
169 dl 1.31 for (;;) {
170     int current = getState();
171     if (current == 0 || compareAndSetState(current, 0))
172     return current;
173     }
174     }
175 dl 1.23 }
176 dl 1.16
177 tim 1.1 /**
178 dl 1.34 * NonFair version
179     */
180     final static class NonfairSync extends Sync {
181     NonfairSync(int permits) {
182     super(permits);
183     }
184    
185     protected int tryAcquireShared(int acquires) {
186     return nonfairTryAcquireShared(acquires);
187     }
188     }
189    
190     /**
191     * Fair version
192     */
193     final static class FairSync extends Sync {
194     FairSync(int permits) {
195     super(permits);
196     }
197    
198     protected int tryAcquireShared(int acquires) {
199     Thread current = Thread.currentThread();
200     for (;;) {
201 dl 1.35 Thread first = getFirstQueuedThread();
202     if (first != null && first != current)
203 dl 1.34 return -1;
204     int available = getState();
205     int remaining = available - acquires;
206     if (remaining < 0 ||
207     compareAndSetState(available, remaining))
208     return remaining;
209     }
210     }
211     }
212    
213     /**
214 dl 1.41 * Creates a <tt>Semaphore</tt> with the given number of
215 dl 1.36 * permits and nonfair fairness setting.
216     * @param permits the initial number of permits available. This
217     * value may be negative, in which case releases must
218     * occur before any acquires will be granted.
219     */
220     public Semaphore(int permits) {
221     sync = new NonfairSync(permits);
222     }
223    
224     /**
225 dl 1.41 * Creates a <tt>Semaphore</tt> with the given number of
226 dl 1.19 * permits and the given fairness setting.
227 dl 1.16 * @param permits the initial number of permits available. This
228     * value may be negative, in which case releases must
229     * occur before any acquires will be granted.
230     * @param fair true if this semaphore will guarantee first-in
231     * first-out granting of permits under contention, else false.
232 tim 1.1 */
233 dl 1.16 public Semaphore(int permits, boolean fair) {
234 dl 1.34 sync = (fair)? new FairSync(permits) : new NonfairSync(permits);
235 tim 1.1 }
236    
237     /**
238     * Acquires a permit from this semaphore, blocking until one is
239     * available, or the thread is {@link Thread#interrupt interrupted}.
240     *
241     * <p>Acquires a permit, if one is available and returns immediately,
242     * reducing the number of available permits by one.
243     * <p>If no permit is available then the current thread becomes
244     * disabled for thread scheduling purposes and lies dormant until
245     * one of two things happens:
246     * <ul>
247     * <li>Some other thread invokes the {@link #release} method for this
248 dl 1.16 * semaphore and the current thread is next to be assigned a permit; or
249 tim 1.1 * <li>Some other thread {@link Thread#interrupt interrupts} the current
250     * thread.
251     * </ul>
252     *
253     * <p>If the current thread:
254     * <ul>
255     * <li>has its interrupted status set on entry to this method; or
256     * <li>is {@link Thread#interrupt interrupted} while waiting
257     * for a permit,
258     * </ul>
259     * then {@link InterruptedException} is thrown and the current thread's
260     * interrupted status is cleared.
261     *
262     * @throws InterruptedException if the current thread is interrupted
263     *
264     * @see Thread#interrupt
265     */
266 dl 1.2 public void acquire() throws InterruptedException {
267 dl 1.23 sync.acquireSharedInterruptibly(1);
268 dl 1.2 }
269 tim 1.1
270     /**
271     * Acquires a permit from this semaphore, blocking until one is
272     * available.
273     *
274     * <p>Acquires a permit, if one is available and returns immediately,
275     * reducing the number of available permits by one.
276     * <p>If no permit is available then the current thread becomes
277     * disabled for thread scheduling purposes and lies dormant until
278     * some other thread invokes the {@link #release} method for this
279 dl 1.16 * semaphore and the current thread is next to be assigned a permit.
280 tim 1.1 *
281     * <p>If the current thread
282     * is {@link Thread#interrupt interrupted} while waiting
283     * for a permit then it will continue to wait, but the time at which
284     * the thread is assigned a permit may change compared to the time it
285     * would have received the permit had no interruption occurred. When the
286     * thread does return from this method its interrupt status will be set.
287     *
288     */
289 dl 1.2 public void acquireUninterruptibly() {
290 dl 1.39 sync.acquireShared(1);
291 dl 1.2 }
292 tim 1.1
293     /**
294     * Acquires a permit from this semaphore, only if one is available at the
295     * time of invocation.
296     * <p>Acquires a permit, if one is available and returns immediately,
297     * with the value <tt>true</tt>,
298     * reducing the number of available permits by one.
299     *
300     * <p>If no permit is available then this method will return
301     * immediately with the value <tt>false</tt>.
302     *
303 dl 1.27 * <p>Even when this semaphore has been set to use a
304     * fair ordering policy, a call to <tt>tryAcquire()</tt> <em>will</em>
305     * immediately acquire a permit if one is available, whether or not
306     * other threads are currently waiting.
307     * This &quot;barging&quot; behavior can be useful in certain
308     * circumstances, even though it breaks fairness. If you want to honor
309     * the fairness setting, then use
310     * {@link #tryAcquire(long, TimeUnit) tryAcquire(0, TimeUnit.SECONDS) }
311     * which is almost equivalent (it also detects interruption).
312     *
313 tim 1.1 * @return <tt>true</tt> if a permit was acquired and <tt>false</tt>
314     * otherwise.
315     */
316     public boolean tryAcquire() {
317 dl 1.34 return sync.nonfairTryAcquireShared(1) >= 0;
318 tim 1.1 }
319    
320     /**
321     * Acquires a permit from this semaphore, if one becomes available
322     * within the given waiting time and the
323     * current thread has not been {@link Thread#interrupt interrupted}.
324     * <p>Acquires a permit, if one is available and returns immediately,
325     * with the value <tt>true</tt>,
326     * reducing the number of available permits by one.
327     * <p>If no permit is available then
328     * the current thread becomes disabled for thread scheduling
329     * purposes and lies dormant until one of three things happens:
330     * <ul>
331     * <li>Some other thread invokes the {@link #release} method for this
332 dl 1.16 * semaphore and the current thread is next to be assigned a permit; or
333 tim 1.1 * <li>Some other thread {@link Thread#interrupt interrupts} the current
334     * thread; or
335     * <li>The specified waiting time elapses.
336     * </ul>
337     * <p>If a permit is acquired then the value <tt>true</tt> is returned.
338     * <p>If the current thread:
339     * <ul>
340     * <li>has its interrupted status set on entry to this method; or
341     * <li>is {@link Thread#interrupt interrupted} while waiting to acquire
342     * a permit,
343     * </ul>
344     * then {@link InterruptedException} is thrown and the current thread's
345     * interrupted status is cleared.
346     * <p>If the specified waiting time elapses then the value <tt>false</tt>
347     * is returned.
348 dl 1.16 * If the time is less than or equal to zero, the method will not wait
349     * at all.
350 tim 1.1 *
351     * @param timeout the maximum time to wait for a permit
352 dl 1.13 * @param unit the time unit of the <tt>timeout</tt> argument.
353 tim 1.1 * @return <tt>true</tt> if a permit was acquired and <tt>false</tt>
354     * if the waiting time elapsed before a permit was acquired.
355     *
356     * @throws InterruptedException if the current thread is interrupted
357     *
358     * @see Thread#interrupt
359     *
360     */
361 dl 1.16 public boolean tryAcquire(long timeout, TimeUnit unit)
362 tim 1.1 throws InterruptedException {
363 dl 1.39 return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
364 tim 1.1 }
365    
366     /**
367     * Releases a permit, returning it to the semaphore.
368     * <p>Releases a permit, increasing the number of available permits
369     * by one.
370     * If any threads are blocking trying to acquire a permit, then one
371     * is selected and given the permit that was just released.
372     * That thread is re-enabled for thread scheduling purposes.
373     * <p>There is no requirement that a thread that releases a permit must
374     * have acquired that permit by calling {@link #acquire}.
375     * Correct usage of a semaphore is established by programming convention
376     * in the application.
377     */
378 dl 1.2 public void release() {
379 dl 1.23 sync.releaseShared(1);
380 dl 1.16 }
381    
382     /**
383     * Acquires the given number of permits from this semaphore,
384     * blocking until all are available,
385     * or the thread is {@link Thread#interrupt interrupted}.
386     *
387     * <p>Acquires the given number of permits, if they are available,
388     * and returns immediately,
389     * reducing the number of available permits by the given amount.
390     *
391     * <p>If insufficient permits are available then the current thread becomes
392     * disabled for thread scheduling purposes and lies dormant until
393     * one of two things happens:
394     * <ul>
395     * <li>Some other thread invokes one of the {@link #release() release}
396     * methods for this semaphore, the current thread is next to be assigned
397     * permits and the number of available permits satisfies this request; or
398     * <li>Some other thread {@link Thread#interrupt interrupts} the current
399     * thread.
400     * </ul>
401     *
402     * <p>If the current thread:
403     * <ul>
404     * <li>has its interrupted status set on entry to this method; or
405     * <li>is {@link Thread#interrupt interrupted} while waiting
406     * for a permit,
407     * </ul>
408     * then {@link InterruptedException} is thrown and the current thread's
409     * interrupted status is cleared.
410 dl 1.19 * Any permits that were to be assigned to this thread are instead
411 dl 1.16 * assigned to the next waiting thread(s), as if
412     * they had been made available by a call to {@link #release()}.
413     *
414     * @param permits the number of permits to acquire
415     *
416     * @throws InterruptedException if the current thread is interrupted
417     * @throws IllegalArgumentException if permits less than zero.
418     *
419     * @see Thread#interrupt
420     */
421     public void acquire(int permits) throws InterruptedException {
422     if (permits < 0) throw new IllegalArgumentException();
423 dl 1.23 sync.acquireSharedInterruptibly(permits);
424 dl 1.16 }
425    
426     /**
427     * Acquires the given number of permits from this semaphore,
428     * blocking until all are available.
429     *
430     * <p>Acquires the given number of permits, if they are available,
431     * and returns immediately,
432     * reducing the number of available permits by the given amount.
433     *
434     * <p>If insufficient permits are available then the current thread becomes
435     * disabled for thread scheduling purposes and lies dormant until
436     * some other thread invokes one of the {@link #release() release}
437     * methods for this semaphore, the current thread is next to be assigned
438     * permits and the number of available permits satisfies this request.
439     *
440     * <p>If the current thread
441     * is {@link Thread#interrupt interrupted} while waiting
442     * for permits then it will continue to wait and its position in the
443     * queue is not affected. When the
444     * thread does return from this method its interrupt status will be set.
445     *
446     * @param permits the number of permits to acquire
447     * @throws IllegalArgumentException if permits less than zero.
448     *
449     */
450     public void acquireUninterruptibly(int permits) {
451     if (permits < 0) throw new IllegalArgumentException();
452 dl 1.39 sync.acquireShared(permits);
453 dl 1.16 }
454    
455     /**
456 dl 1.40 * Acquires the given number of permits from this semaphore, only
457     * if all are available at the time of invocation.
458 dl 1.41 *
459 dl 1.16 * <p>Acquires the given number of permits, if they are available, and
460     * returns immediately, with the value <tt>true</tt>,
461     * reducing the number of available permits by the given amount.
462     *
463     * <p>If insufficient permits are available then this method will return
464     * immediately with the value <tt>false</tt> and the number of available
465     * permits is unchanged.
466     *
467 dl 1.27 * <p>Even when this semaphore has been set to use a fair ordering
468     * policy, a call to <tt>tryAcquire</tt> <em>will</em>
469     * immediately acquire a permit if one is available, whether or
470     * not other threads are currently waiting. This
471     * &quot;barging&quot; behavior can be useful in certain
472     * circumstances, even though it breaks fairness. If you want to
473     * honor the fairness setting, then use {@link #tryAcquire(int,
474     * long, TimeUnit) tryAcquire(permits, 0, TimeUnit.SECONDS) }
475     * which is almost equivalent (it also detects interruption).
476     *
477 dl 1.16 * @param permits the number of permits to acquire
478     *
479     * @return <tt>true</tt> if the permits were acquired and <tt>false</tt>
480     * otherwise.
481     * @throws IllegalArgumentException if permits less than zero.
482     */
483     public boolean tryAcquire(int permits) {
484     if (permits < 0) throw new IllegalArgumentException();
485 dl 1.34 return sync.nonfairTryAcquireShared(permits) >= 0;
486 dl 1.16 }
487    
488     /**
489     * Acquires the given number of permits from this semaphore, if all
490     * become available within the given waiting time and the
491     * current thread has not been {@link Thread#interrupt interrupted}.
492     * <p>Acquires the given number of permits, if they are available and
493     * returns immediately, with the value <tt>true</tt>,
494     * reducing the number of available permits by the given amount.
495     * <p>If insufficient permits are available then
496     * the current thread becomes disabled for thread scheduling
497     * purposes and lies dormant until one of three things happens:
498     * <ul>
499     * <li>Some other thread invokes one of the {@link #release() release}
500     * methods for this semaphore, the current thread is next to be assigned
501     * permits and the number of available permits satisfies this request; or
502     * <li>Some other thread {@link Thread#interrupt interrupts} the current
503     * thread; or
504     * <li>The specified waiting time elapses.
505     * </ul>
506     * <p>If the permits are acquired then the value <tt>true</tt> is returned.
507     * <p>If the current thread:
508     * <ul>
509     * <li>has its interrupted status set on entry to this method; or
510     * <li>is {@link Thread#interrupt interrupted} while waiting to acquire
511     * the permits,
512     * </ul>
513     * then {@link InterruptedException} is thrown and the current thread's
514     * interrupted status is cleared.
515     * Any permits that were to be assigned to this thread, are instead
516     * assigned to the next waiting thread(s), as if
517     * they had been made available by a call to {@link #release()}.
518     *
519     * <p>If the specified waiting time elapses then the value <tt>false</tt>
520     * is returned.
521     * If the time is
522     * less than or equal to zero, the method will not wait at all.
523     * Any permits that were to be assigned to this thread, are instead
524     * assigned to the next waiting thread(s), as if
525     * they had been made available by a call to {@link #release()}.
526     *
527     * @param permits the number of permits to acquire
528     * @param timeout the maximum time to wait for the permits
529     * @param unit the time unit of the <tt>timeout</tt> argument.
530     * @return <tt>true</tt> if all permits were acquired and <tt>false</tt>
531     * if the waiting time elapsed before all permits were acquired.
532     *
533     * @throws InterruptedException if the current thread is interrupted
534     * @throws IllegalArgumentException if permits less than zero.
535     *
536     * @see Thread#interrupt
537     *
538     */
539     public boolean tryAcquire(int permits, long timeout, TimeUnit unit)
540     throws InterruptedException {
541     if (permits < 0) throw new IllegalArgumentException();
542 dl 1.39 return sync.tryAcquireSharedNanos(permits, unit.toNanos(timeout));
543 dl 1.16 }
544    
545     /**
546     * Releases the given number of permits, returning them to the semaphore.
547     * <p>Releases the given number of permits, increasing the number of
548     * available permits by that amount.
549     * If any threads are blocking trying to acquire permits, then the
550 dl 1.19 * one that has been waiting the longest
551 dl 1.16 * is selected and given the permits that were just released.
552     * If the number of available permits satisfies that thread's request
553     * then that thread is re-enabled for thread scheduling purposes; otherwise
554     * the thread continues to wait. If there are still permits available
555     * after the first thread's request has been satisfied, then those permits
556     * are assigned to the next waiting thread. If it is satisfied then it is
557     * re-enabled for thread scheduling purposes. This continues until there
558     * are insufficient permits to satisfy the next waiting thread, or there
559     * are no more waiting threads.
560     *
561     * <p>There is no requirement that a thread that releases a permit must
562     * have acquired that permit by calling {@link Semaphore#acquire acquire}.
563     * Correct usage of a semaphore is established by programming convention
564     * in the application.
565     *
566     * @param permits the number of permits to release
567     * @throws IllegalArgumentException if permits less than zero.
568     */
569     public void release(int permits) {
570     if (permits < 0) throw new IllegalArgumentException();
571 dl 1.23 sync.releaseShared(permits);
572 dl 1.2 }
573 tim 1.1
574     /**
575 dl 1.41 * Returns the current number of permits available in this semaphore.
576 tim 1.1 * <p>This method is typically used for debugging and testing purposes.
577     * @return the number of permits available in this semaphore.
578     */
579 dl 1.16 public int availablePermits() {
580 dl 1.29 return sync.getPermits();
581 tim 1.1 }
582 dl 1.15
583     /**
584 dl 1.31 * Acquire and return all permits that are immediately available.
585     * @return the number of permits
586     */
587     public int drainPermits() {
588     return sync.drainPermits();
589     }
590    
591     /**
592 dl 1.41 * Shrinks the number of available permits by the indicated
593 dl 1.40 * reduction. This method can be useful in subclasses that use
594     * semaphores to track resources that become unavailable. This
595     * method differs from <tt>acquire</tt> in that it does not block
596     * waiting for permits to become available.
597 dl 1.15 * @param reduction the number of permits to remove
598     * @throws IllegalArgumentException if reduction is negative
599     */
600 dl 1.16 protected void reducePermits(int reduction) {
601 dl 1.15 if (reduction < 0) throw new IllegalArgumentException();
602 dl 1.29 sync.reducePermits(reduction);
603 dl 1.16 }
604    
605     /**
606 dl 1.42 * Returns true if this semaphore has fairness set true.
607 dl 1.16 * @return true if this semaphore has fairness set true.
608     */
609     public boolean isFair() {
610 dl 1.34 return sync instanceof FairSync;
611 dl 1.22 }
612    
613     /**
614 dl 1.23 * Queries whether any threads are waiting to acquire. Note that
615     * because cancellations may occur at any time, a <tt>true</tt>
616     * return does not guarantee that any other thread will ever
617     * acquire. This method is designed primarily for use in
618     * monitoring of the system state.
619     *
620     * @return true if there may be other threads waiting to acquire
621     * the lock.
622 dl 1.22 */
623 dl 1.25 public final boolean hasQueuedThreads() {
624     return sync.hasQueuedThreads();
625 dl 1.22 }
626    
627     /**
628 dl 1.23 * Returns an estimate of the number of threads waiting to
629     * acquire. The value is only an estimate because the number of
630     * threads may change dynamically while this method traverses
631     * internal data structures. This method is designed for use in
632     * monitoring of the system state, not for synchronization
633     * control.
634     * @return the estimated number of threads waiting for this lock
635 dl 1.22 */
636 dl 1.23 public final int getQueueLength() {
637     return sync.getQueueLength();
638 dl 1.22 }
639    
640     /**
641 dl 1.23 * Returns a collection containing threads that may be waiting to
642     * acquire. 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.
648     * @return the collection of threads
649 dl 1.22 */
650 dl 1.23 protected Collection<Thread> getQueuedThreads() {
651     return sync.getQueuedThreads();
652 dl 1.16 }
653 dl 1.37
654     /**
655 dl 1.40 * Returns a string identifying this semaphore, as well as its state.
656 dl 1.38 * The state, in brackets, includes the String
657     * &quot;Permits =&quot; followed by the number of permits.
658     * @return a string identifying this semaphore, as well as its
659     * state
660 dl 1.37 */
661     public String toString() {
662     return super.toString() + "[Permits = " + sync.getPermits() + "]";
663     }
664    
665 tim 1.1 }