.NET 的 ManualResetEvent 和 WaitHandle 的 Java 等效项
我想知道 Java 是否提供了 .NET 的 ManualResetEvent 和 WaitHandle 类的等效项,因为我想编写在给定超时时间内阻塞的代码,除非触发事件。
.NET 类 WaitHandle 和 ManualResetEvent 提供了一个很好的、无麻烦的接口,据我所知,它也是线程安全的,那么 Java 必须提供什么呢?
I would like to know if Java provides an equivalent of .NET's classes of ManualResetEvent and WaitHandle, as I would like to write code that blocks for a given timeout unless an event is triggered.
The .NET classes of WaitHandle and ManualResetEvent provide a nice, hassle-free interface for that which is also thread-safe as far as I know, so what does Java has to offer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否考虑过使用
wait
/notify
(相当于Monitor.Wait
和Monitor.Pulse
)?您需要进行一些检查,看看您是否确实需要等待(以避免竞争条件),但它应该可以工作。
否则,类似于
CountDownLatch
很可能会做你想做的事。编辑:我刚刚注意到 CountDownLatch 基本上是“一次性使用” - 据我所知,以后您无法重置计数。您可能需要
Semaphore
相反。像这样使用tryAcquire
来等待超时:请注意,这与
ManualResetEvent
不同,每次成功调用tryAcquire
都会减少许可数量- 所以最终他们会再次耗尽。您无法像使用ManualResetEvent
那样将其永久“设置”。 (这可以与 CountdownLatch 一起使用,但是你无法“重置”它:)Have you considered using
wait
/notify
(the equivalent ofMonitor.Wait
andMonitor.Pulse
) instead?You'll want a little bit of checking to see whether you actually need to wait (to avoid race conditions) but it should work.
Otherwise, something like
CountDownLatch
may well do what you want.EDIT: I've only just noticed that
CountDownLatch
is basically "single use" - you can't reset the count later, as far as I can see. You may wantSemaphore
instead. UsetryAcquire
like this to wait with a timeout:Note that this is unlike
ManualResetEvent
in that each successful call totryAcquire
will reduce the number of permits - so eventually they'll run out again. You can't make it permanently "set" like you could withManualResetEvent
. (That would work withCountdownLatch
, but then you couldn't "reset" it :)来自:http://www.experts-exchange.com/Programming/ Languages/Java/Q_22076798.html
您好,您可以使用 java.util.concurrent.Semaphore 类实现同步(使用 0 许可)。
http://java.sun. com/j2se/1.5.0/docs/api/java/util/concurrent/Semaphore.html
下面的示例向您展示了如何解决第一个同步问题,其他同步问题将类似:
From: http://www.experts-exchange.com/Programming/Languages/Java/Q_22076798.html
Hi, you can achieve synchronization using the java.util.concurrent.Semaphore class (use 0 permit).
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Semaphore.html
Example below shows you how to solve the first sync problem, the other will be similar:
理论上,上面给出的 ManualResetEvent 类在 Java 5 上是正确的(但不是更早版本)。鉴于 volatile 的错误(或不充分)实现的悠久历史,对我来说,在 reset() 中添加额外的同步块以生成有保证的写入屏障并确保完整的原子性似乎更明智。危险在于,在多处理器 Intel cpu 上,“open”的读取可能会传递“open”的写入。下面给出的更改的优点:它可能不是最佳效率,但它确实具有保证不会出错的巨大优点,而且只需很少的额外成本。
感谢原海报。
In theory, the ManualResetEvent class as given above is correct on Java 5 (but not earlier). Given the long history of incorrect (or inadequate) implementations of volatile, it seems wiser to me to add an additional synchronized block in reset() in order to generate a guaranteed write barrier, and ensure complete atomicity. The danger is that a read of "open" may pass a write of "open" on multi-processor Intel cpus. The advantage of the change given below: it may not be optimally efficient, but it does have the great advantage of being guaranteed to be not wrong, at very little additional cost.
Thanks to the original poster.