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

发布于 2024-09-28 14:02:42 字数 80 浏览 4 评论 0原文

当线程无法获取信号量(由于缺乏许可证)时会发生什么。是否会转入等待状态?

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

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 execution sequence, when the semaphore becomes available.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟的代价 2024-10-05 14:02:42

当线程无法获取信号量(由于缺乏许可)时会发生什么。会转入等待状态吗?

是的。如果您正在谈论 java. util.concurrent.Semaphore (和aquire 方法 发生的情况是:

从此信号量获取许可,阻塞直到有可用信号量,或者线程被中断。

[...]

如果没有可用的许可,则当前线程将出于线程调度目的而被禁用,并处于休眠状态,直到发生以下两种情况之一:

  • 某个其他线程调用该信号量的release()方法,并且当前线程接下来将被分配一个许可;或

  • 其他线程中断当前线程。

tryAquire<然而,顾名思义, /code>只会尝试获取锁,如果没有许可,则不会阻塞返回 false。

当信号量可用时,线程是否会开始恢复之前的执行顺序。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文