I was getting java.lang.IllegalMonitorStateException
. I referred this question and it solved my problem. The first answer is
To be able to call notify() you need to synchronize on the same object.
synchronized (someObject) {
someObject.wait();
}
/* different thread / object */
synchronized (someObject) {
someObject.notify();
}
My question is why we need to synchronize on the same object ad how it works?
As far as my understanding goes when we say
synchronized (someObject) {
someObject.wait();
}
we get a lock on object someObject and then we call wait() on it. Now how can another thread get lock on same object to call notify() on it? What am I missing?