Class Semaphore

java.lang.Object
edu.wpi.first.util.concurrent.Semaphore
All Implemented Interfaces:
AutoCloseable

public final class Semaphore
extends Object
implements AutoCloseable
A semaphore for synchronization.

Semaphores keep an internal counter. Releasing the semaphore increases the count. A semaphore with a non-zero count is considered signaled. When a waiter wakes up it atomically decrements the count by 1. This is generally useful in a single-supplier, multiple-consumer scenario.

  • Constructor Details

    • Semaphore

      public Semaphore​(int initialCount, int maximumCount)
      Constructor.
      Parameters:
      initialCount - initial value for the semaphore's internal counter
      maximumCount - maximum value for the semaphore's internal counter
    • Semaphore

      public Semaphore​(int initialCount)
      Constructor. Maximum count is Integer.MAX_VALUE.
      Parameters:
      initialCount - initial value for the semaphore's internal counter
    • Semaphore

      public Semaphore()
      Constructor. Initial count is 0, maximum count is Integer.MAX_VALUE.
  • Method Details

    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • getHandle

      public int getHandle()
      Gets the semaphore handle (e.g. for waitForObject).
      Returns:
      handle
    • release

      public boolean release​(int releaseCount)
      Releases N counts of the semaphore.
      Parameters:
      releaseCount - amount to add to semaphore's internal counter; must be positive
      Returns:
      True on successful release, false on failure (e.g. release count would exceed maximum value, or handle invalid)
    • release

      public boolean release()
      Releases 1 count of the semaphore.
      Returns:
      True on successful release, false on failure (e.g. release count would exceed maximum value, or handle invalid)