File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait and notifyAll Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait and notifyAll " Watch "wait and notifyAll " New topic
Author

wait and notifyAll

Afzal Rehman
Ranch Hand

Joined: Dec 16, 2009
Posts: 39


Hi
This Question is from ExamLab ..
Output is Ready to Print, Now Printing.

I am little bit confused in use of wait and notify and notifyAll. why it's not sending notification to above wait...
and if i want to send notification to a above wait then what i suppose to change and why?

Many Thanks


SCJP 6.0
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Afzal Rehman wrote:
Hi
This Question is from ExamLab ..
Output is Ready to Print, Now Printing.

I am little bit confused in use of wait and notify and notifyAll. why it's not sending notification to above wait...
and if i want to send notification to a above wait then what i suppose to change and why?

Many Thanks



t.notifyAll() will send a notification to all threads waiting for object "t". Here wait is not called on object t, so the notification will never come and it will go to an infinite wait..
If you change the code to t.wait() on a synchronized (t), the t.notifyAll() will give notification to wait() and the wait() ends. please try the code below:



SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Afzal Rehman
Ranch Hand

Joined: Dec 16, 2009
Posts: 39
Thanks Rajeev
Andre Enimot
Ranch Hand

Joined: Jul 29, 2009
Posts: 31
In your original line 13, wait() is called on "this", which is totally fine. But it didn't work as you expected because t in line 17 refers not to instance of Trd referenced by that "this", but to instance of Thread. If you introduce reference Trd t0=new Trd() and use t0 in line 22 & 24 instead of t, it will work.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066



Try this, Hope you will understand!

wait(), notify(0, notifyAll() should be called for the same object, else, there are communication between Threads.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait and notifyAll
 
Similar Threads
Thread question doubt
wait() not syncronized on this but not throwing illegalMonitorStateException
Thread
Thread Question from Examlab
Thread question