aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question regarding notify() and wait() 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 "Question regarding notify() and wait()" Watch "Question regarding notify() and wait()" New topic
Author

Question regarding notify() and wait()

Jeff Schuler
Greenhorn

Joined: Apr 17, 2007
Posts: 23
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

Joined: Sep 15, 2006
Posts: 134
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
kathir vel
Ranch Hand

Joined: Jul 03, 2007
Posts: 92
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

Joined: Jul 03, 2007
Posts: 92
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

Joined: Apr 17, 2007
Posts: 23
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Feb 22, 2007
Posts: 285
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....
 
jQuery in Action, 2nd edition
 
subject: Question regarding notify() and wait()