ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Semaphore.java
Revision: 1.65
Committed: Tue Dec 23 11:58:00 2014 UTC (9 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.64: +9 -4 lines
Log Message:
Documentation improvements

File Contents

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