开发者

What happens to a Thread that fails to acquire a Semaphore?

开发者 https://www.devze.com 2023-01-21 12:09 出处:网络
What happens when a thread cannot acquire a Semaphore (due to lack of permit). Will it be moved to the wait state?

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 executi开发者_Go百科on 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消