views:

62

answers:

2

Hello fellow StackOverflowers (<--bad pun), 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 even is triggered.

The .NET classes of WaitHandle and ManualResetEvent provide an nice, hassle-free interface for that which is also thread-safe as far as I know, so what does Java has to offer?

+2  A: 

Have you considered using wait/notify (the equivalent of Monitor.Wait and Monitor.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 want Semaphore instead. Use tryAcquire like this to wait with a timeout:

if (semaphore.tryAquire(5, TimeUnit.SECONDS)) {
   ...
   // Permit was granted before timeout
} else {
   // We timed out while waiting
}

Note that this is unlike ManualResetEvent in that each successful call to tryAcquire will reduce the number of permits - so eventually they'll run out again. You can't make it permanently "set" like you could with ManualResetEvent. (That would work with CountdownLatch, but then you couldn't "reset" it :)

Jon Skeet
But what about the timeout aspect? From what I read in CountDownLatch's documentation, it doesn't seem to have a timeout value that I can intercept or stop before the timeout runs to the end. I would like to have a Java type that I can use to wait on for a timeout, and that can be intercepted before the timeout finishes, like a manual override.Also, hello Mr. Skeet! How are you doing? You probably get this a lot, but your book is great!
Voulnet
@Voulnet: You'd call `await(long timeout, TimeUnit unit)` - that will block for the given timeout duration *or* return earlier if the latch is counted down to 0. Glad you like the book :)
Jon Skeet
Yup, the solution must work beautifully since I only have one object waiting at any time. Thanks, Mr. Jon Skeet.
Voulnet
+2  A: 

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:

import java.util.concurrent.Semaphore;

class ScalesCommunication {

   private static Semaphore sem = new Semaphore(0);

   // called by thread 1
   void readLoop() {
      //...

      //after connection established, release semaphore (value incremented by 1)
      sem.release();
   }

   // called by thread 2
   String sendCommand(String command) {

       sem.acquire(); // thread waits here if sem value == 0

       // at this point connection is established
       //...
   }
}
Preet Sangha