Semaphore (int allow, boolean fair), what does justice do?

I need to implement a FIFO queue if there are no permissions available on the semaphore. My professor pointed out that during the exam we can only use methods acquire()and release(). My idea was to implement LinkedList using a method tryAcquire()that, if the result was false, would add currentThread to the bottom of the list. It is not possible to use the method tryAcquire()that I was looking for, and I noticed this construct. If I initialize the semaphore, for example:

Semaphore example = new Semaphore(5, true);

What happens if a thread tries to use a method acquire()on a semaphore that has exhausted its permissions? Does it create an automatic queue? Should I handle it in any way or automatic process?

+4
source share
1 answer

is not an explicit java doctor that way?

It says that a call to Thread acquirewill be blocked until a permission is available, and not tryAcquirethat returns false - this means that there are no permissions, but Thread calling this method will not be blocked.

Justice is connected. If there are already pending threads, but you are tryAcquirethrough the current thread, it will ignore justice and get this permission. Since you cannot use it, justice is the FIFO queue you are looking for. Literally from the document:

​​, , , , , (first-in-first-out, FIFO)

, - release Thread - , , .

+2

Source: https://habr.com/ru/post/1692576/


All Articles