Firstly, i know that it seems like this question has been asked before, and it did but i didn't get any good answer so i will ask again more clearly.
This code may cause deadlock if the notify happens when no other thread is currently waiting
while (!checkPreConditions()){
synchronized(lock){
lock.wait();
}
}
doWork();
synchronized(lock) {
lock.notifyAll();
}
I'v tried putting a counter for the number of threads notifying while no threads are waiting but this solution was a bit cumbersome..
another idea was to timeout the wait but then the program may wait for no reason.
is there a common solution / pattern for that problem?