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 Threads - Notify question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads - Notify question" Watch "Threads - Notify question" New topic
Author

Threads - Notify question

robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37
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?



SCJP 1.5
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
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

Joined: Jun 02, 2008
Posts: 37
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

Joined: Nov 20, 2007
Posts: 192
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

Joined: Mar 21, 2007
Posts: 71
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 .


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads - Notify question
 
Similar Threads
notify thread
Threads
Threads
lock on Classname.class?
Thread