加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 19:12:38 所属栏目:Java 来源:网络整理
导读:当线程无法获取信号量时(由于缺少许可证)会发生什么.它会被转移到等待状态吗? 编辑:当信号量可用时,线程是否会恢复先前的执行序列. 最佳答案 What happens when a thread cannot acquire a Semaphore (due to lack of permit). Will it be moved to the wa

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

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

最佳答案

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返回并继续执行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读