开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜