Believe it or not ... wait()
and notify()
work just fine.
The problem is that you are not using them correctly.
while(checkValues()==true){
System.out.println(name+" waiting");
wait();
}
notify();
This is going to wait until some other thread notifies the this
instance for the method call. That cannot be the notify();
call after the loop ...'cos you cannot get there until you've been notified.
Now it is possible that some other thread could be doing that notify()
, but you haven't shown us the code that does that. And the current code doesn't indicate that. And the symptoms you describe point that way too.
Unfortunately, I cannot figure out what your code is actually trying to do. It really doesn't make much sense to me. So I can't suggest how to fix it.
The best I can do is suggest that you read some tutorial examples of the right way to use wait/notify. For instance: