gpt4 book ai didi

java - 在许 cocoa 用之前,Semaphore.tryAcquire(...) 会阻塞还是自旋?

转载 作者:行者123 更新时间:2023-11-29 03:42:04 24 4
gpt4 key购买 nike

如果不公平的信号量许可不是立即可用的,tryAcquire(long timeout, TimeUnit unit) 会阻塞直到许 cocoa 用(即让出执行),还是旋转直到许 cocoa 用?

如果调用旋转并且系统有一堆信号量,系统是否会面临有大量旋转信号量的风险?

最佳答案

根据 javadoc:

If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three 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; or
  • The specified waiting time elapses

这表明线程被标记为阻塞(作为灰色状态放入阻塞队列),这允许处理器执行其他工作。

关于java - 在许 cocoa 用之前,Semaphore.tryAcquire(...) 会阻塞还是自旋?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12708871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com