Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes wait(), notify, notifyAll() doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait(), notify, notifyAll() doubt" Watch "wait(), notify, notifyAll() doubt" New topic
Author

wait(), notify, notifyAll() doubt

sunil shah
Greenhorn

Joined: Mar 13, 2010
Posts: 6
Hi,

The other day I was going through Kathy sierra/Bert bates book on SCJP and came across the code which is given as an example of wait/notify/notifyAll(chapter 9, code with classes - Reader and Calculator.). But the weird thing is that even if I remove the call to notify/notifyAll, the waiting threads come out of wait stage. Is this a case of spurious wake-up? I tried it on both windows and Linux machines but the results are more or less same.

Any help on this will be greatly appreciated.

Thanks,
Sunil
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

Try posting this question on the author's site.


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
sunil shah
Greenhorn

Joined: Mar 13, 2010
Posts: 6
Hi Edward,

Could you please share the author's website URL? I tried searching it but could not find

Thanks in advance,

Sunil
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40


Please do a search, as most of K&B related questions has already been asked.

Also, it may help if you actually post the code, since not everyone have K&B handy.


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sunil shah
Greenhorn

Joined: Mar 13, 2010
Posts: 6
Hi Henry,

I have searched for it but could not find any similar topic.

Following is the code I am talking about.

Please note that I have added a few println statements for my understanding.Also while loop for checking the value of variable c.finished is intentionally commented out.


public class Reader extends Thread
{
Calculator c;

public Reader(Calculator calc)
{
c = calc;
}

public void run() {
synchronized(c)
{
try
{
System.out.println("Waiting for calculation...");
//while(!c.finished)
c.wait();

System.out.println("Resume processing...");
}
catch (InterruptedException e) {System.out.println("Exception..."+e);}

}
System.out.println("Total is: " + c.total);
}

public static void main(String [] args)
{
Calculator calculator = new Calculator();

//calculator.start();
for(int i=0; i<20;i++)
{

new Reader(calculator).start();
}

calculator.start();

}
}


class Calculator extends Thread
{
int total;
int count;
boolean finished = false;

public void run() {
System.out.println(Thread.currentThread().getName()+" "+"Entered run method");
synchronized(this)
{

System.out.println(Thread.currentThread().getName()+" "+"Entered sync block");
for(int i=0;i<100;i++)
{
total += i;
}
//finished = true;
//notifyAll();
}
System.out.println(Thread.currentThread().getName()+" "+"Exiting run method");
}

}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

sunil shah wrote:
I have searched for it but could not find any similar topic.


See...

http://www.coderanch.com/t/233952/Threads-Synchronization/java/Threading-discrepancies


Summary: The Java library uses wait() and notifyAll() all on the thread object, to internally implement join() -- so don't use the thread object object as your notification object because of this.

Henry
sunil shah
Greenhorn

Joined: Mar 13, 2010
Posts: 6
Thanks a lot henry!! now it's crystal clear..........

I somehow missed the "thread discrepancies" post.....my sincere appologies for that.....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40


Other somewhat related topics (if you are interested)...

http://www.coderanch.com/t/416345/Threads-Synchronization/java/notify-mandatory
http://www.coderanch.com/t/234368/Threads-Synchronization/java/wait-without-notify
http://www.coderanch.com/t/233142/Threads-Synchronization/java/synchronized-keywords-static-methods

Henry
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Below is a revised version of the code that you posted. It launches all the
MyReader threads and then starts one Calculator. Calculator then counts
to 100, sets ready=true, notifies the listeners and pauses to let a waiting
thread run. It then counts again, once for each MyReader thread. If you
take out the yield() statement, you can see the calculator rush ahead.
Some notify() calls may get lost and MyReader threads will be left waiting. Notice how changing the 'ready' value is synchronized together with the
wait() and notify() calls. Also, the idiom of "while(condition==false) wait();"
should always be used. (I'm quoting from the Sun documentation.) Reset
of the condition variable after the wait() is important too.

Jim... ...


BEE MBA PMP SCJP-6
 
jQuery in Action, 2nd edition
 
subject: wait(), notify, notifyAll() doubt