What happens when a thread cannot acquire a Semaphore (due to lack of permit). Will it be moved to the wait state?
EDIT:Will the thread start resume the previous execution sequence, when the semaphore becomes available.
What happens when a thread cannot acquire a Semaphore (due to lack of permit). Will it be moved to the wait state?
Yes. If you're talking about java.util.concurrent.Semaphore
(and the aquire
method this is what happens:
Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted.
[...]
If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
Some other thread invokes the release() method for this semaphore and the current thread is next to be assigned a permit; or
Some other thread interrupts the current thread.
tryAquire
will however, as the name suggests, only try to aquire the lock, and instead of blocking return false if it has no permit.
Will the thread start resume the previous execution sequence, when the semaphore becomes available.
Yes. If another thread calls release
this thread may return from acquire
and continue it's execution.
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