r.v
r.v

Reputation: 4877

calling Object.notify() before Object.wait()

If there is no thread which is waiting, using Object.wait() , any calls to Object.notify() or Object.notifyAll() have no effect. I have a scenario in which if I call Object.notify() when the wait set is empty, a subsequent call to Object.wait() should not put the thread to wait. How can this be accomplished? Semaphores may be one solution I can think of. Is there a more elegant solution?

Upvotes: 5

Views: 3361

Answers (4)

shabby
shabby

Reputation: 3222

I implemented it like this

Thread A:

 req.run();
synchronized (req) {
                    try {
                        req.wait(3000);
                        rawResponse = eq.ReturnXML;
                        logger.info("[" + refID + "] Response recieved: " + rawResponse);
                        responseRecieved = true;
                        req.notify();
                    } catch (InterruptedException ex) {
                        logger.error("Error waiting on req", ex);
                    }
                }

Thread B:

synchronized (req) {
        while (!responseRecieved) {
            try {
                req.wait(3000);
            } catch (InterruptedException ex) {
                logger.error("Error waiting on req while trying to get state", ex);
            }
        }
    }

Thread A makes the request and waits for a response, in the meanwhile Thread B is only waiting for the response. If a response has already arrived it does not wait.

Upvotes: 0

Konrad Garus
Konrad Garus

Reputation: 54035

This kind of scenario seems to be a perfect fit for a Semaphore. Call Semaphore.release() instead of notify() and Semaphore.acquire() instead of wait.

Upvotes: 7

Jon Skeet
Jon Skeet

Reputation: 1502406

I would use Semaphore, CyclicBarrier or possibly CountDownLatch - whichever is a better fit for your real scenario. I think it's a good idea to reuse existing abstractions rather than use the low-level mechanisms yourself, unless those mechanisms give you exactly the behaviour you want (which they don't in this case).

Upvotes: 4

John Vint
John Vint

Reputation: 40256

Use a flag to indicating a notification. Read the flag before entering wait and act accordingly.

boolean stopped = false;

public void run(){
   synchronized(object){
      while(!stopped)
        object.wait();
   }

}

public void stop(){
  synchronized(object){
    stopped=true;
    object.notify();
  }

}

Upvotes: 7

Related Questions