什么是 java 的 ManualResetEvent 等价物?等价物、java、ManualResetEvent

2023-09-08 09:43:23 作者:沉淀期待未来

ManualResetEvent 的 java 等价物是什么?

What is java's equivalent of ManualResetEvent?

推荐答案

我所知道的最接近的是 信号量.只需将其与许可"计数 1 一起使用,获取/释放将与您从 ManualResetEvent 中知道的几乎相同.

The closest I know of is the Semaphore. Just use it with a "permit" count of 1, and aquire/release will be pretty much the same as what you know from the ManualResetEvent.

初始化为 1 的信号量,并且使用它使其仅具有大多数许可证可用,可以服务作为互斥锁.这是通常称为二进制信号量,因为它只有两个状态:一个许可证可用,或零可用的许可证.用于此方式,二进制信号量有属性(不像许多 Lock实现),锁"可以由线程以外的线程释放所有者(因为信号量没有所有权).这在某些情况下可能很有用专门的上下文,例如死锁恢复.

A semaphore initialized to one, and which is used such that it only has at most one permit available, can serve as a mutual exclusion lock. This is more commonly known as a binary semaphore, because it only has two states: one permit available, or zero permits available. When used in this way, the binary semaphore has the property (unlike many Lock implementations), that the "lock" can be released by a thread other than the owner (as semaphores have no notion of ownership). This can be useful in some specialized contexts, such as deadlock recovery.