• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Threads - Notify question

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic