I have a doubt about Synchronized and wait working together...in following code-
class Q{
int n;
booolean flag=flase;
void Synchronized get(){
if (!flag) try{
wait();
}
catch(Exception e){}
flag=true;
notify();
return n;
}
void Synchronized put(int n){....}
}
wait() has to be called within Synchronized block.Synchronized will lock the instance of Q .when wait() method is called, it releases lock on current object. Now how come the method is in synch?
while this
thread waits because of wait() call, it releases lock on Q, and another thread can aquire that lock and update data of Q. Then how the purpose of Synchronized is achieved?