Package org.jgrapes.io.util
Class PermitsPool
java.lang.Object
org.jgrapes.io.util.PermitsPool
A class that manages a set of permits and notifies listeners
about changes of availability.
Listeners are added using WeakReference
, so removing
them isn’t strictly necessary.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionacquire()
Acquire a permit, waiting until one becomes available.addListener
(AvailabilityListener listener) Adds an AvailabilityListener.augmentPermits
(int permits) Adds the given number of permits to the pool.int
Returns the number of currently available permits.reducePermits
(int permits) Remove the given number of permits from the pool.release()
Release a previously obtained permit.removeListener
(AvailabilityListener listener) Removes the listener.boolean
Try to acquire a permit.
-
Constructor Details
-
PermitsPool
Instantiates a new permits pool.- Parameters:
permits
- the permits
-
-
Method Details
-
availablePermits
Returns the number of currently available permits.- Returns:
- the result
-
augmentPermits
Adds the given number of permits to the pool.- Parameters:
permits
- the number of permits to add- Returns:
- the permits pool
-
reducePermits
Remove the given number of permits from the pool.- Parameters:
permits
- the number of permits to remove- Returns:
- the permits pool
-
addListener
Adds an AvailabilityListener.- Parameters:
listener
- the AvailabilityListener- Returns:
- the permits pool
-
removeListener
Removes the listener.- Parameters:
listener
- the AvailabilityListener- Returns:
- the permits pool
-
release
Release a previously obtained permit. -
acquire
Acquire a permit, waiting until one becomes available.- Returns:
- the permits pool
- Throws:
InterruptedException
- the interrupted exception
-
tryAcquire
Try to acquire a permit.- Returns:
true
if successful
-