• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Question regarding notify() and wait()

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the following code, I thought that the main thread would wait forever because the notify() statement is never run. Instead the "Total is" line was displayed. Does anyone know why?

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah could be. but if you use WAIT() overload version then it would interrupt as time goes down. here still it would be a guess saying b would run first. you can't since it's JVM dependent. but seems like if you don't notify then MAIN would get chance to run.
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait() and notify() is used internally -- to implement the join() method. Basically, a notifyAll() is sent on the thread object, when a thread terminates, in order to wakeup all the threads waiting to join.

Henry
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ThreadA is waiting(give up the lock to ThreadB) for ThreadB notify(),i.e to get object lock again.After ThreadB loop codes completion the lock will be released by ThreadB.So again threadA can get it back with out notification.
 
kathir vel
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this sample program also the same logic...with out notify waiting thread can continue execution once it get the object lock again.

class Reader extends Thread {
Calculator c;

public Reader(Calculator calc) {
c = calc;
}

public void run() {
synchronized(c) {
try {
System.out.println("Waiting for calculation...");
c.wait();
} catch (InterruptedException e) {}
System.out.println("Total is: " + c.total);
}
}

public static void main(String [] args) {
Calculator calculator = new Calculator();

new Reader(calculator).start();
new Reader(calculator).start();
new Reader(calculator).start();
calculator.start();
}
}

class Calculator extends Thread {
int total;

public void run() {
synchronized(this) {
for(int i=0;i<100;i++) {
total += i;
}
System.out.println("caluculate total is "+total);
// notifyAll();
}
}
}
 
Jeff Schuler
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by kathir vel:
In this sample program also the same logic...with out notify waiting thread can continue execution once it get the object lock again.



According to the K&B book the program will wait forever even if the lock is released. It does mention that sometimes the thread will sometimes start on it's own even without a notify() depending on the JVM. I wonder if this is the reason.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeff Schuler:
According to the K&B book the program will wait forever even if the lock is released. It does mention that sometimes the thread will sometimes start on it's own even without a notify() depending on the JVM. I wonder if this is the reason.

It's the same as with your example. The calculator thread calls notifyAll() on itself when it dies. But as you suppose, it will not work in the general case. look at the following example:Without the notify, the second thread will wait forever.
 
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI i have same concern .In K&B self test Answer explanation note, its written that once a thread finds wait() method it will wait forever if nobody call notify but in Khalid mughal its written that if notify() is not called for a particular period ,then waiting thread will wake up and it will not know the difference whether some one notify it or it wakes up itself....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!