GeezerVison is showing docs.oracle.com — Semaphore (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.util.concurrent
Class Semaphore
-
-
Constructor Summary
Constructors
| Constructor |
Description |
Semaphore(int permits) |
Creates a Semaphore with the given number of
permits and nonfair fairness setting.
|
Semaphore(int permits,
boolean fair) |
Creates a Semaphore with the given number of
permits and the given fairness setting.
|
-
Method Summary
All Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
void |
acquire() |
Acquires a permit from this semaphore, blocking until one is
available, or the thread is interrupted.
|
void |
acquire(int permits) |
Acquires the given number of permits from this semaphore,
blocking until all are available,
or the thread is interrupted.
|
void |
acquireUninterruptibly() |
Acquires a permit from this semaphore, blocking until one is
available.
|
void |
acquireUninterruptibly(int permits) |
Acquires the given number of permits from this semaphore,
blocking until all are available.
|
int |
availablePermits() |
Returns the current number of permits available in this semaphore.
|
int |
drainPermits() |
Acquires and returns all permits that are immediately available.
|
protected Collection<Thread> |
getQueuedThreads() |
Returns a collection containing threads that may be waiting to acquire.
|
int |
getQueueLength() |
Returns an estimate of the number of threads waiting to acquire.
|
boolean |
hasQueuedThreads() |
Queries whether any threads are waiting to acquire.
|
boolean |
isFair() |
Returns true if this semaphore has fairness set true.
|
protected void |
reducePermits(int reduction) |
Shrinks the number of available permits by the indicated
reduction.
|
void |
release() |
Releases a permit, returning it to the semaphore.
|
void |
release(int permits) |
Releases the given number of permits, returning them to the semaphore.
|
String |
toString() |
Returns a string identifying this semaphore, as well as its state.
|
boolean |
tryAcquire() |
Acquires a permit from this semaphore, only if one is available at the
time of invocation.
|
boolean |
tryAcquire(int permits) |
Acquires the given number of permits from this semaphore, only
if all are available at the time of invocation.
|
boolean |
tryAcquire(int permits,
long timeout,
TimeUnit unit) |
Acquires the given number of permits from this semaphore, if all
become available within the given waiting time and the current
thread has not been interrupted.
|
boolean |
tryAcquire(long timeout,
TimeUnit unit) |
Acquires a permit from this semaphore, if one becomes available
within the given waiting time and the current thread has not
been interrupted.
|
-
-
Constructor Detail
-
Semaphore
public Semaphore(int permits,
boolean fair)
Creates a Semaphore with the given number of
permits and the given fairness setting.
- Parameters:
permits - the initial number of permits available.
This value may be negative, in which case releases
must occur before any acquires will be granted.
fair - true if this semaphore will guarantee
first-in first-out granting of permits under contention,
else false
-
Method Detail
-
release
public void release()
Releases a permit, returning it to the semaphore.
Releases a permit, increasing the number of available permits by
one. If any threads are trying to acquire a permit, then one is
selected and given the permit that was just released. That thread
is (re)enabled for thread scheduling purposes.
There is no requirement that a thread that releases a permit must
have acquired that permit by calling acquire().
Correct usage of a semaphore is established by programming convention
in the application.
-
reducePermits
protected void reducePermits(int reduction)
Shrinks the number of available permits by the indicated
reduction. This method can be useful in subclasses that use
semaphores to track resources that become unavailable. This
method differs from acquire in that it does not block
waiting for permits to become available.
- Parameters:
reduction - the number of permits to remove
- Throws:
IllegalArgumentException - if reduction is negative
-
hasQueuedThreads
public final boolean hasQueuedThreads()
Queries whether any threads are waiting to acquire. Note that
because cancellations may occur at any time, a true
return does not guarantee that any other thread will ever
acquire. This method is designed primarily for use in
monitoring of the system state.
- Returns:
true if there may be other threads waiting to
acquire the lock
-
getQueueLength
public final int getQueueLength()
Returns an estimate of the number of threads waiting to acquire.
The value is only an estimate because the number of threads may
change dynamically while this method traverses internal data
structures. This method is designed for use in monitoring of the
system state, not for synchronization control.
- Returns:
- the estimated number of threads waiting for this lock
-
getQueuedThreads
protected Collection<Thread> getQueuedThreads()
Returns a collection containing threads that may be waiting to acquire.
Because the actual set of threads may change dynamically while
constructing this result, the returned collection is only a best-effort
estimate. The elements of the returned collection are in no particular
order. This method is designed to facilitate construction of
subclasses that provide more extensive monitoring facilities.
- Returns:
- the collection of threads
-
toString
public String toString()
Returns a string identifying this semaphore, as well as its state.
The state, in brackets, includes the String "Permits ="
followed by the number of permits.
- Overrides:
toString in class Object
- Returns:
- a string identifying this semaphore, as well as its state
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|