Multithreading - specifics on wait() and notify()
807607Oct 1 2006 — edited Oct 1 2006I'm not new to Java but I am new to multithreading and concurrency in Java.
Here's my problem. I have multiple threads competing for access to a shared resource. We will assume that all the threads are identical for simplicity.
If I have a shared class resource (not the thread class) that the threads are competing for, how can I effectively use wait() and notify() to effect this? Within the shared object, there is a method I call to determine if it's safe to access. It returns (from a synchronized block) iff it is safe to access. Otherwise, it waits. Now, I don't know where to put the notify(). The object will not be made available for some set time after. At that time, what I want is for the remaining threads to wake up (using notify()) and try again.
I can't call notify immediately before returning, because they will go back to waiting. If I try to call it when the initial thread is complete (say, after 5 seconds) I presumably need to do this from within the shared object, correct?