Dinakar Kas wrote:
Now, what happens if I try to acquire a permit by saying sem.acquire();??
Keep in mind, that the
Java Semaphore class is not a lock (although it can be used to simulate a lock). It's just a simple counter that holds permits. Any
thread can acquire a permit, and any number of permits. Any thread can release any number of permits. And there are no checks to confirm that a thread that releases a permit actually acquired it.
So, it's perfectly reasonable to have a semaphore with an initial permit count of zero, or even negative. And the semaphore won't allow any thread to acquire any permits until the count becomes positive.
Henry