File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with notify/ notifyAll method

 
Kishore Kumar
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This code is from the K&B,





OUTPUT:
Waiting for calculation...
Waiting for calculation...
Waiting for calculation...
Total is: 4950
Total is: 4950
Total is: 4950

Well, this is OK... But even when I comment the notifyAll() method call,
I get the same output.. Actually i expected the output to be
OUTPUT:
Waiting for calculation...
Waiting for calculation...
Waiting for calculation...

and the program never stops since there is no notify/notifyAll method called. Instead i get t he same out put. Can any one please tell me why it is so?
 
Mamadou Touré
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi kesava,

In this case that's normal behavior , even if you comment the notifyAll, because, after the run() method, the thread Calculator is dead, so all the thread waiting for him will resume their execution, even if they are not notified. In other words : Reader's thread will wait for Caclculator only if this one is Alive, otherwise there is no need to wait.

Hope this will help
 
Kishore Kumar
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



Then i tried with this example. If i remove comment line#1. Program is executed completely. but if i comment line #1, program waits for the thread's notification. Can you explain why this is so here.

 
Mamadou Touré
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, it's because the object you're waiting on (obj.wait()) is not a Thread object but just a MyThread object.

I modified your code as following (to make it wait on a thread), and It will behave exactly as your first example (ie if you uncomment the notifyAll, the thread will resume execution)


class MyThread implements Runnable {

public void run() {
try {
synchronized (this) {
//System.out.println("Lock Released");
System.out.println("Lock Attained1 on " + this.toString());
// notifyAll(); //line #1
System.out.println("Lock Released1 on " + this.toString() );
}System.out.println("This Thread has completed" );
}catch(Exception e)
{e.printStackTrace();
}
}
public String toString()
{return "MyThread";
}
}
class RunThis implements Runnable
{
MyThread obj = null;
Thread tt = null;
public void run()
{
try
{
// synchronized(obj)
synchronized(tt)
{
System.out.println("Lock Attained on " + obj.toString());
//obj.wait();
tt.wait();
System.out.println("Lock Released on "+obj.toString());
}}
catch(Exception e)
{e.printStackTrace();
}
}
public String toString()
{
return "RunThis";
}
public static void main(String[] args)
{
MyThread obj = new MyThread();
RunThis obj1 = new RunThis();
obj1.obj = obj;
Thread t = new Thread(obj);
obj1.tt = t;
Thread t1 = new Thread(obj1);
t1.start();
t.start();
}
}

[ May 06, 2008: Message edited by: Mamadou Tour� ]
[ May 06, 2008: Message edited by: Mamadou Tour� ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic