Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 959
r.v Avatar asked Jul 05 '11 15:07

r.v


1 Answers

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.

like image 84
Konrad Garus Avatar answered Sep 25 '22 16:09

Konrad Garus