Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait(), notify, notifyAll() doubt

 
sunil shah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try posting this question on the author's site.
 
sunil shah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 22116
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
sunil shah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 22116
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot henry!! now it's crystal clear..........

I somehow missed the "thread discrepancies" post.....my sincere appologies for that.....
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic