Class PermitsPool

java.lang.Object
org.jgrapes.io.util.PermitsPool

public class PermitsPool extends Object
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 Details

    • PermitsPool

      public PermitsPool(int permits)
      Instantiates a new permits pool.
      Parameters:
      permits - the permits
  • Method Details

    • availablePermits

      public int availablePermits()
      Returns the number of currently available permits.
      Returns:
      the result
    • augmentPermits

      public PermitsPool augmentPermits(int permits)
      Adds the given number of permits to the pool.
      Parameters:
      permits - the number of permits to add
      Returns:
      the permits pool
    • reducePermits

      public PermitsPool reducePermits(int permits)
      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

      public PermitsPool 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

      public boolean tryAcquire()
      Try to acquire a permit.
      Returns:
      true if successful