A Semaphore (counting Semaphore) is a concurrency control construct. It conforms
to the standard acquire-release protocol. Counting Semaphores are widely used in
implementation of multi-threaded bounded resource pools, collections etc.
A Semaphore maintains a set of totalPermits initialized in a constructor. Method acquire()
blocks in wait till a permit becomes available and then takes it. The release() method needs
to be invoked to add the permit back to the Semaphore and notify a waiting
thread that a permit has become available to use. Method attemptAcquire() is
the same as acquire() except it fails on time-out. It returns true or false
based on whether it succeeded or failed.
This implementation of Semaphore is non-reentrant i.e. if the a thread that has a
permit re-enters to acquire one more permit, it would be given another permit if
available.
|