Package edu.wpi.first.util.concurrent
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
Constructor.- Parameters:
initialCount
- initial value for the semaphore's internal countermaximumCount
- maximum value for the semaphore's internal counter
-
Semaphore
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
- Specified by:
close
in interfaceAutoCloseable
-
getHandle
Gets the semaphore handle (e.g. for waitForObject).- Returns:
- handle
-
release
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
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)
-