What is java's equivalent of ManualResetEvent?
class ManualResetEvent { private final Object monitor = new Object(); private volatile boolean open = false; public ManualResetEvent(boolean open) { this.open = open; } public void waitOne() throws InterruptedException { synchronized (monitor) { while (open==false) { monitor.wait(); } } } public boolean waitOne(long milliseconds) throws InterruptedException { synchronized (monitor) { if (open) return true; monitor.wait(milliseconds); return open; } } public void set() {//open start synchronized (monitor) { open = true; monitor.notifyAll(); } } public void reset() {//close stop open = false; } }
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
.
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.
Try CountDownLatch with count of one.
CountDownLatch startSignal = new CountDownLatch(1);
Based on:
ManualResetEvent allows threads to communicate with each other by signaling. Typically, this communication concerns a task which one thread must complete before other threads can proceed.
from here:
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx
you possibly want to look at the Barriers in the Java concurrency package - specifically CyclicBarrier I believe:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CyclicBarrier.html
It blocks a fixed number of threads until a particular event has occured. All the threads must come together at a barrier point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With