Can I add more permit to a semaphore in Java?
Semaphore s = new Semaphore(3);
After this somewhere in the code i want to change the permits to 4. Is this possible?
Yes. The release method (confusingly named imo) can be used to increment permits since, from the docs:
There is no requirement that a thread that releases a permit must
have acquired that permit by calling acquire.
Correct usage of a semaphore is established by programming convention
in the application.
In other words:
semaphore.release(10);
Will add 10 more permits if the thread calling hasn't acquired any.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With