Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait and notifyAll

 
Afzal Rehman
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Afzal Rehman
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rajeev
 
Andre Enimot
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Try this, Hope you will understand!

wait(), notify(0, notifyAll() should be called for the same object, else, there are communication between Threads.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic