• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronized and wait() together.......

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic