Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads - Notify question

 
robert stannard
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers, can you please help me with this question. The program just prints "trying to notify" but it doesn't tell the "wait()" thread in "run()" method to continue and so it doesn't print "notified".
Apparently the reason is that the thread which issues the "NotifyAll" doesnt own the lock on the same thread that the Wait() is holding.
Do you know how I can modify the synchronize statement in "startme()" so that it also owns a lock on the wait() thread and so will allow that thread to continue?

 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The run method is waiting on 'this', while startMe has a lock on the class, nobody to notify.

Put a sleep of about 1 second into startMe and you'll see that wait is called first, then notifyAll but "Notified" is never printed.

Here's the code for that.


Now try it locking on the instance...


or both on the class...

[ June 17, 2008: Message edited by: Taariq San ]
 
robert stannard
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Taariq,

Thank you thats excellent. I tried both your solutions and they both worked perfectly.
Whats a confusing for me is that your solutions can only work by adding in the "Thread.sleep(1000);" line. I guess this is to ensure that that the notifyAll() doesnt run before the wait() has had a chance to secure a lock on that object?

Regards
Robert.
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by robert stannard:
Hi Taariq,

Thank you thats excellent. I tried both your solutions and they both worked perfectly.
Whats a confusing for me is that your solutions can only work by adding in the "Thread.sleep(1000);" line. I guess this is to ensure that that the notifyAll() doesnt run before the wait() has had a chance to secure a lock on that object?

Regards
Robert.


You're welcome, and 100% right. If you remove the sleep the output is...
Trying to Notify
Waiting

It takes a few milliseconds longer to get the thread going and by that time restart method has been called already.

I tested it with Thread.sleep(1); and it worked, but I ran it on Windows which isn't accurate down to the millisecond, so 1 millisecond is about 10 milliseconds. Anyhat, seems to need just that breather to get going.
 
Madhukar Ojha
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancher ,

you must always keep into mind that object lock and class lock are independent to each other .

You have called wait() method on object and notifyAll() has been called
on class level lock .

Call notifyAll() on same object OR call wait() on class level lock .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic