Semaphore

A semaphore maintains a fixed number of threads in executions if exceeds lock can be acquired for that semaphore thread.

Below method will get the lock of semaphore within semaphore.availablePermits()

If there are more that fixed threads, it will wait to acquire/get the semaphore lock until it has semaphore.availablepermits() greater than ‘0’.
A semaphore initialised to one, has only one permit available is a mutual exclusion lock, which we can say as “mutex“.
This is more commonly known as a binary semaphore, as it has 2 states, zero permit and one permit, and lock can be released from other threads.

Result:

Leave a Reply

Your email address will not be published. Required fields are marked *