aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with notify/ notifyAll method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt with notify/ notifyAll method" Watch "Doubt with notify/ notifyAll method" New topic
Author

Doubt with notify/ notifyAll method

Kishore Kumar
Ranch Hand

Joined: Oct 15, 2007
Posts: 71
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

Joined: Dec 27, 2007
Posts: 189
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


SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
--------------------
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
Kishore Kumar
Ranch Hand

Joined: Oct 15, 2007
Posts: 71
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

Joined: Dec 27, 2007
Posts: 189
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� ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt with notify/ notifyAll method