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.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
int
getHandle()
Gets the semaphore handle (e.g.boolean
release()
Releases 1 count of the semaphore.boolean
release(int releaseCount)
Releases N counts of the semaphore.
-
-
-
Constructor Detail
-
Semaphore
public Semaphore(int initialCount, int maximumCount)
Constructor.- Parameters:
initialCount
- initial value for the semaphore's internal countermaximumCount
- maximum value for the samephore'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 Detail
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
-
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)
-
-