Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding notify() and wait()

 
Jeff Schuler
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?

 
chintan ramavat
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
 
kathir vel
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.
 
Manfred Klug
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.
 
raj malhotra
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....
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic