I was reading about wait and notify in Java.
I will explain with a small example:
@Override
public void run() {
synchronized (msg) {
try{
msg.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(name+" processed: "+msg.getMsg());
}
}
Here it says that when we do synchronized (msg)
. The current thread T1 will take a lock on the msg Object.
So the lock can be released via 2 ways :
- After the synchronized block is completed
- when the wait() is called by T1 ??
And if some another thread calls notify() and woke up the the thread T1, T1 will again gain access to lock on msg
object?