Monday, April 30, 2007
Explain wait(),notify(), and notifyAll() methods?
Subscribe to:
Post Comments (Atom)
World of tricky Core Java Q&A(Java SE),Java EE and Open source technologies like Struts,Hibernate,Spring,Velocity etc
Disclaimer
Interview Questions On Java,Java EE Copyright © 2012
1 comments:
Hi, Just to add while using wait and notify or notifyAll method in Java following things must be remember :
1) use notifyAll instead of notify if you expect more than one thread is waiting for lock.
2)wait() and notify() method must be called from synchronized context, see the link for more detailed explanation.
3) Always call wait() method in loop because if multiple threads are waiting for lock and one of them got lock and reset the condition and other thread needs to check the condition after they got wake up to see whether they need to wait again or can start processing.
4) use same object for calling wait() and notify() method, every object has its own lock so calling wait() on objectA and notify() on object B will not make any sense.
Post a Comment