I want to start with IllegalMonitorStateException which we get if the current thread is not the owner of the object's monitor. So if I do this, I will get exception:
public class Testing {
Object objLock = new Object();
void dance(){
synchronized (this){
objLock.wait();
}
}
}
So I came to conclusion that you must have same object to synchronize and call wait/notify. Does that mean I can only have one condition per lock?
But then there is Condition class and Lock interface. How do they manage to solve the job?
public class Testing {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
void dance(){
lock.lock();
condition.await();
lock.unlock();
}
}
Before I learn something wrong, does this mean that Lock/Condition example allows us to have more conditions? And how come when I just showed example of IllegalMonitorStateException which prevents us from doing exactly that. Can someone please explain my confusion? How did Condition class 'trick it'? Or did it, if I said something wrong?