Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of Semaphore.drainPermits()?

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()?

like image 252
volni Avatar asked Dec 03 '12 22:12

volni


People also ask

What does semaphore release do?

Exits the semaphore a specified number of times and returns the previous count.

Are semaphores First In First Out?

The semaphore waiting queue is First-In First-Out (FIFO).

When called the method Acquire () of the Java semaphore class will?

acquire. Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted.

Can semaphore be negative Java?

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.


1 Answers

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
like image 124
JB Nizet Avatar answered Oct 02 '22 11:10

JB Nizet