ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Semaphore.java
Revision: 1.63
Committed: Sun Oct 21 06:40:20 2012 UTC (11 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.62: +0 -1 lines
Log Message:
no blank line between javadoc and corresponding code

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