• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Doubt with notify/ notifyAll method

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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� ]
reply
    Bookmark Topic Watch Topic
  • New Topic