Semaphore Class Flashcards
Constructor to create Semaphore objects with a given number of permits (the number of threads that can access the resource at a time). If the permit’s value is negative, the given number of release() calls must happen before acquire() calls can succeed.
Semaphore(int permits)
Same as the Semaphore(int permits) constructor, but this extra fair option indicates that the permits should be allotted on a first come first served basis.
Semaphore(int permits, boolean fair)
Acquires a permit if available; blocks until a permit becomes available. Can throw an InterruptedException if some other thread interrupts it while waiting to acquire a permit. Overloaded version takes a number of permits to acquire as an argument.
void acquire()
Same as the acquire() method but the thread cannot be interrupted while waiting to acquire a permit.
void acquireUninterruptibly()
Acquires a permit from the semaphore if it is available at the time of the call and returns true; if unavailable, it returns false immediatly (without blocking). Overloaded version takes a timeout period. It will block trying to acquire the permit until the timeout period.
boolean tryAcquire()
Releases a permit from the semaphore. The overloaded version specifies the number of permits to release.
void release()