public int drainPermits():
Acquires and returns all permits that are immediately available. Returns: the number of permits acquired.
Why would someone want to acquire and then immediately release all available permits from a Semaphore? If they want to see the number of available permits, why not use Semaphore.availablePermits()
?
Exits the semaphore a specified number of times and returns the previous count.
The semaphore waiting queue is First-In First-Out (FIFO).
acquire. Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted.
Java Semaphore is initialized with an integer to set the number of permits, negative integers are not allowed. Binary semaphores can only compute one process at a time, thus no multiprocessing. The integer value can only be either 0 or 1, and it is initialised to be 1.
It doesn't release them. It acquires them all, that's all:
Semaphore s = new Semaphore(10);
System.out.println(s.availablePermits()); // 10
s.drainPermits();
System.out.println(s.availablePermits()); // 0
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