I am reading the book Java Concurrency in Practice. In a section about java.util.concurrent.Semaphore
, the below lines are present in the book. It is a comment abou
Perhaps the last line " a Semaphore is not limited to the number of permits it was created with" is your source of confusion.
A semaphore when created is initialized with a fixed set of permits. This then becomes the maximum number of permits that the semaphore can simultaneuosly dispense at any time during the life time of that semaphore. You cannot dynamically increase this number except by re-initializing the semaphore .
The meaning if the quoted line ( from JCIP ) is this : First , the semantics of how a semaphore works is not limited to the details of issuing and regaining a permit - this is manifested in the fact that any thread can that has access the semaphore can have a permit released ( even though this thread did not own the permit at the first place)
Second , you can dynamically reduce the maximum permits of a semaphore - by calling reducePermits(int)
method.