Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Doubt with notify/ notifyAll method  RSS feed

 
Ranch Hand
Posts: 71
  • 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?
 
Ranch Hand
Posts: 189
  • 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
  • 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
  • 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� ]
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!