• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

After notify()

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you get answer for my question posted above?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve for your valuable information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic