开发者

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

开发者 https://www.devze.com 2023-03-17 23:35 出处:网络
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() whe

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?


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.


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();
  }

}


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消