I was solving Leetcode problem 1114. I was using wait( ) and notify( ) concept. Using wait() giving time limit exceed but using notifyAll() is giving correct answer.
Why using notify() giving TLE and with notifyAll() it is working fine ??
class Foo {
private boolean oneDone;
private boolean twoDone;
public Foo() {
oneDone = false;
twoDone = false;
}
public synchronized void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
oneDone = true;
//notify();
notifyAll();
}
public synchronized void second(Runnable printSecond) throws InterruptedException {
while (!oneDone) {
wait();
}
printSecond.run();
twoDone = true;
//notify();
notifyAll();
}
public synchronized void third(Runnable printThird) throws InterruptedException {
while (!twoDone) {
wait();
}
printThird.run();
}
}
Why notify() giving TLE but notifyAll() not giving TLE ?