java – 无法获取信号量的线程会发生什么?

前端之家收集整理的这篇文章主要介绍了java – 无法获取信号量的线程会发生什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当线程无法获取信号量时(由于缺少许可证)会发生什么.它会被转移到等待状态吗?

编辑:当信号量可用时,线程是否会恢复先前的执行序列.

最佳答案

What happens when a thread cannot acquire a Semaphore (due to lack of permit). Will it be moved to the wait state?

是.如果你在谈论java.util.concurrent.Semaphore(和the aquire method这是发生的事情:

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只会尝试获取锁,而不是在没有许可的情况下阻止返回false.

Will the thread start resume the prevIoUs execution sequence,when the semaphore becomes available.

是.如果另一个线程调用release,则该线程可以从acquire返回并继续执行.

原文链接:https://www.f2er.com/java/437735.html

猜你在找的Java相关文章