The Semaphore Value: What It Really Means
Positive Value: Available Permits
When the value is positive (say, 3), it means 3 more threads can call wait() and proceed immediately without blocking. These are available permits waiting to be claimed.
Zero Value: Fully Occupied
When the value is 0, all permits are taken. No threads are waiting yet, but the next thread to call wait() will block. The resource pool is exactly at capacity.
Negative Value: Waiting Queue
When the value is negative (say, -2), it means 2 threads are blocked, waiting in queue. All permits are taken AND there is a backlog. The absolute value tells you the queue length.
You Cannot Read the Value
In most implementations, you cannot directly check the semaphore value. This is by design - the value could change between reading and acting on it. You can only wait() and signal().
Debugging tip: Some implementations offer a tryWait() that returns immediately (success or failure) without blocking. Useful for testing but not for production logic.