aspose file tools*
The moose likes Threads and Synchronization and the fly likes Using wait/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 "Using wait/notify()" Watch "Using wait/notify()" New topic
Author

Using wait/notify()

Gil Steidle
Ranch Hand

Joined: Nov 19, 2010
Posts: 30
Greetings,

I am not understanding what the notify() method is doing. In the example, I am expecting the the notify() to wake up ThreadA and let it resume and print the total (which is 21 at the time of notification). Instead, ThreadA always prints 45.

Thanks for your help.




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

I'm surprised too. Your code contains all kinds of debugging output but you didn't say anything about that in your problem description. I can only conclude that it didn't appear. And that surprises me.
Gil Steidle
Ranch Hand

Joined: Nov 19, 2010
Posts: 30
I should have included this originally, sorry. This is the output I am seeing.

C:\JAVA\SCJP\ch9>java ThreadA
0
1
3
6
10
15
21
***Notified***
28
36
45
Waiting for Thread b to complete...
Total: 45

But I am expecting something like:

C:\JAVA\SCJP\ch9>java ThreadA
0
1
3
6
10
15
21
***Notified***
Waiting for Thread b to complete...
Total: 45
28
36
45


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

I would put line 19 before line 18 because then you would know when that thread started to wait. (That would have the side effect of making what you print there correct.) Actually debugging output before and after the wait would be helpful.

Gil Steidle
Ranch Hand

Joined: Nov 19, 2010
Posts: 30


The revised code is above and the results are below. Any thoughts?


C:\JAVA\SCJP\ch9>java ThreadA
Waiting for Thread b to complete.
0
1
3
6
10
15
21
***Notified***
28
36
45
Waiting no more.
Total: 45
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
Are you sure the notify() call is waking the right object? Try notifyAll() instead, and see what happens.

And I think this is beyond "beginning Java" so shall move this thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
notifyAll() didn't make any difference.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

Have a look at this tutorial: http://download.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html.

Especially note this sentence:
That tutorial wrote:Some time after the second thread has released the lock, the first thread reacquires the lock and resumes by returning from the invocation of wait.


And that's what is happening. The second thread calls notify(), or notifyAll(), then it does some other stuff, then when it reaches the end of the synchronized block, it releases the lock. At that time the first thread continues on after its call to wait().
Gil Steidle
Ranch Hand

Joined: Nov 19, 2010
Posts: 30
So, in other words a notify() will not release the lock until the encapsulating synchronized code block is finished?
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 418
    
    2

Yep the notified thread will try to get ownership of the monitor and when it does contine, and wait can wake up without notify so wait's should always be in loops read this and change your code ...

wait JavaDoc


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Gil Steidle
Ranch Hand

Joined: Nov 19, 2010
Posts: 30
Chris, thanks for the link it helped sink this concept in a little more. Am I correct in saying that even without a notify (or notifyAll) that a lock will be released when a synchronized method or code block finishes? I'll run some tests to find out. But can someone confirm or deny?
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 418
    
    2

If you exit a synchronized block or method whatever you were synchronized on is released , was that the question ?? wait / notify doesn't really come into the equation.

The spurious wake up is that effectively the OS say can signal you (notify) in rare circumstances so you can't assume you woke up early because your code called notify / notify all. e.g. (just one example) If you waited for 2 minutes and in that time the system clock changed the OS might (it reserves the right to, not will OS dependent) signal you to give you a chance to react, its up to your code to detect the spurious wake up and react or not. Spurious wake ups are very rare and tend to be seen on Unix variants rather than say Windows.
Sergey Babkin
author
Ranch Hand

Joined: Apr 05, 2010
Posts: 50
Gil Steidle wrote:Chris, thanks for the link it helped sink this concept in a little more. Am I correct in saying that even without a notify (or notifyAll) that a lock will be released when a synchronized method or code block finishes? I'll run some tests to find out. But can someone confirm or deny?


Yes, that's what synchronized does: it locks the object's lock on entrance and unlocks it on exit. It does _not_ unlock for sleep(), or reading from sockets, or such. The only exception is wait(), for the duration of which the lock gets unlocked.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

An example of wait/nofity via Lock and condition object of java.util.concurrent package.
Lock and Condition

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using wait/notify()