I'm not getting the rationale behind negative permits during initialization of Semaphore (java.util.concurrent.Semaphore).
I do know that calls to release() method may eventually make Semaphore's permit greater than equal to one (>=1) so that later on other thread can acquire permit. OR later on during program execution, Semaphore may have negative permits when more threads are awaiting for permits as they executed acquire() method..
However, I don't get any practical use-case where I'd be initializing Semaphore with negative permits.
Sample code for reference:
import java.util.concurrent.Semaphore;
public class SemaphoreDemo {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
Semaphore s = new Semaphore(-2);
System.out.println(s.availablePermits());
s.release(3); // adding 3 permits
System.out.println(s.availablePermits()); // now it has 1 permit
}
}