FairAsyncSemaphore
public interface AsyncSemaphore
Implementations will specify whether their permit acquisition and release is fair or not; this interface does not define this requirement.
Modifier and Type | Method | Description |
---|---|---|
default CompletionStage<Void> |
acquire() |
Acquires 1 permit from the semaphore as if by calling
acquire(long) with an argument
of 1. |
CompletionStage<Void> |
acquire(long permits) |
Acquires the given number of permits from the semaphore, returning a stage which will complete
when all of the permits are exclusively acquired.
|
long |
drainPermits() |
Acquires all permits that are immediately available.
|
long |
getAvailablePermits() |
Gets the number of currently available permits.
|
int |
getQueueLength() |
Gets the number of unfulfilled acquisitions waiting on this semaphore's permits.
|
default void |
release() |
Releases 1 permit from the semaphore as if by calling
release(long) with an argument
of 1. |
void |
release(long permits) |
Releases the given number of permits to the semaphore.
|
default boolean |
tryAcquire() |
Attempts to acquire 1 permit from the semaphore as if by calling
tryAcquire(long) with
an argument of 1. |
boolean |
tryAcquire(long permits) |
Attempts to acquire the given number of permits from the semaphore, returning a boolean
indicating whether all of the permits were immediately available and have been exclusively
acquired.
|
CompletionStage<Void> acquire(long permits)
If the permits are not available immediately, the acquisition will enter a queue and an
incomplete stage will be returned. Semantics of the waiter queue, including ordering policies,
are implementation specific and will be defined by the given implementing class. The returned
stage will complete when sufficient permits have been released
and
assigned to this acquisition by the governing queue policy.
permits
- A non-negative number of permits to acquire from the semaphoreCompletionStage
which will be completed when all permits
have been
acquiredIllegalArgumentException
- if the requested permits are negative, or exceed any
restrictions enforced by the given implementationvoid release(long permits)
If there are unfulfilled acquires pending, this method will release permits to the waiting acquisitions based on the implementation's release and acquire policies. Consequently, this method may complete a number of waiting stages and execute the corresponding observers.
permits
- A non-negative number of permits to release to the semaphoreIllegalArgumentException
- if the released permits are negative, or exceed any
restrictions enforced by the given implementationboolean tryAcquire(long permits)
Implementations may define precise behavior of this method with respect to competing acquisitions, e.g. whether permits may be acquired while other acquisitions are waiting. This interface only requires that this method will succeed when the given permits are available and there are no other acquisitions queued.
permits
- A non-negative number of permits to acquire from the semaphoreIllegalArgumentException
- if the requested permits are negative, or exceed any
restrictions enforced by the given implementationlong drainPermits()
After this call -- provided there are no intermediate releases
-- any
attempt to acquire
will queue and any tryAcquire
will fail.
long getAvailablePermits()
The bounds of the returned value are not defined; an implementation may, for example, choose to represent waiting acquisitions as holding negative permits, and thus the value returned by this method could be negative. Furthermore, a positive number of permits returned by this method may not indicate that such permits are acquirable, as the waiter-queue policy may prohibit fulfilling further acquisitions.
This value is produced on a best-effort basis, and should not be used for any control logic. Generally it is only useful in testing, debugging, or statistics purposes.
int getQueueLength()
This value is produced on a best-effort basis, and should not be used for any control logic. Generally it is only useful in testing, debugging, or statistics purposes.
default CompletionStage<Void> acquire()
acquire(long)
with an argument
of 1.CompletionStage
which will complete when 1 permit has been successfully
acquiredacquire(long)
default void release()
release(long)
with an argument
of 1.release(long)
default boolean tryAcquire()
tryAcquire(long)
with
an argument of 1.tryAcquire(long)
Copyright © 2018. All rights reserved.