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

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...