File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes After notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "After notify()" Watch "After notify()" New topic
Author

After notify()

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

As per my understanding of wait()/notify(), when notify() statement is executed, the lock is released back to the original object that put a wait() on it.
So the original object should start executing (or atleast go back to runnable state ready to execute).

But as per actual experience with the following code, notify() sems to have no effect. Only when the new thread runs its course and dies, does the original thread [one that put the wait() ] gets back control. I tried putting the new thread to sleep() for many seconds, but no effect. Please take a look:

The output is always the same, no matter how many times I run this:-

Ruby Before Notify
Ruby After Notify
Ruby0
Ruby1
Ruby2
Ruby3
Ruby4
main got back lock
main ending


Why??? Why does Ruby thread have to run through its whole course (even with all those sleep() statements) before main would get back control? In that case, I might as well do a join() instead of a wait()/notify().

Would be grateful for any help.


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
I think,

You are getting lock on t (Thread)object but trying to notify on this which is differnt .

here this refers to Stub object not the Thread Object.

Please correct me if i am wrong.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
I think,

You are getting lock on t (Thread)object but trying to notify on this which is differnt .

here this refers to Stub object not the Thread Object.

Try getting lock on "r" instead of t.

Also it is upto JVM to choose next runnable thread.
Please correct me if i am wrong.
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

A-ha! Great explanation. wait() and notify() were indeed synchronizing on different objects. Thanks once again, Siva

Making a few changes, I was able to achieve the desired result.

Here is the updated listing in case anyone runs into the same problem:-



Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
i am wondering how the main thread gets its turn eventhough you did not notify via Thread Object in your previous case?

did you get that point?

i am talking about previous case.

I though the main thread would not stop as no thread notifies it but the output shows main thread got lock back........

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Siva Masilamani wrote:Try getting lock on "r" instead of t.

quote]

Siva,

Just saw the second message you posted. If I get a lock on "r" instead of "t" (which was the first thing I tried), it doesn't work. "t.wait()" gives the IllegalMonitorStateException. It seems that to execute wait() on t, the lock has to be on t
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Nidhi Sar wrote:
Siva Masilamani wrote:Try getting lock on "r" instead of t.

quote]

Siva,

Just saw the second message you posted. If I get a lock on "r" instead of "t" (which was the first thing I tried), it doesn't work. "t.wait()" gives the IllegalMonitorStateException. It seems that to execute wait() on t, the lock has to be on t

You have to use "r" on both wait and synchronized block.then it should be OK.


Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Siva Masilamani wrote:
You have to use "r" on both wait and synchronized block.then it should be OK.


Yes, this works. Didn't think of doing a wait() on the Runnable instead of the Thread !!!
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
did you get answer for my question posted above?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Siva Masilamani wrote:i am wondering how the main thread gets its turn eventhough you did not notify via Thread Object in your previous case?

did you get that point?

i am talking about previous case.

I though the main thread would not stop as no thread notifies it but the output shows main thread got lock back........




This is one of the side effects of the Thread class. When a Thread dies, it calls notifyAll() on its monitor. This is undocumented so it can't and shouldn't be relied on, but it does occur and if you aren't careful it can lead to unpredictable behavior. I chalk it up to one reason to never synchronize on a Thread object, and one of several reasons not to subclass Thread.


Steve
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Thanks Steve for your valuable information.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: After notify()