aspose file tools*
The moose likes Threads and Synchronization and the fly likes wait() without notify() or notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait() without notify() or notifyAll()" Watch "wait() without notify() or notifyAll()" New topic
Author

wait() without notify() or notifyAll()

Prabath Siriwardena
Greenhorn

Joined: Mar 27, 2004
Posts: 15
Dear Friends...!
What will happen if we use a wait() without having notify() call in the programme.
According to the theory the thread waiting shoud wait forever until another
thread notifies on that object, which the previous thread is waiting for .
But in the following programme I coudn't see any differenecs in the out put
when I use wait() with and without notify();
Please tell me why ?
Here is the code...!!!
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(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;
}
// notify();
}

}
}


<a href="http://psiriwardena.blogspot.com/" target="_blank" rel="nofollow">http://psiriwardena.blogspot.com/</a><br />B.Sc(Eng),SCJP,SCWCD,SCBCD,SCDJWS,<br />MCSD,OCA,CCNA.BCS,ACS
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Prabath,
I played with this a bit. I noticed that the Calculator class is also a Thread. I can't find anything to support this, but I suspect that since it's run() method completes and it dies, perhaps it is no longer meaningful to "wait()" on the monitor for the Thread object??? Strange to me, because the Calculator Thread object is still around, since there's a reference to it in Reader.main()).... I verified that the Object was still good in main(), even after the Reader threads had stopped wait()ing and completed, because I was still able to call the getName() method. But it seems that the wait()ing threads all get notified.
I changed your program to eliminate the "extends Thread" clause from the Calculator class definition, and I also changed the call to calculator.start() to calculator.run() to match the name of the defined method. I saw the behavior that I expected (and evidently what you were expecting as well):
When Calculator was not a Thread, without the call to notify(), the Readers never got notified, and they all hung in their wait() calls. I added back your notify(), and at first it appeared that the same thing was happening. But I think it was because (on my machine at least...) the Calculator was getting to the notify() before the Readers got to their wait()s. So I added a sleep(1000L) in main() before the calculator.run(), and with the notify() in place, one of the Reader threads got notified, and finished. Changing it to notifyAll() gave the expected result that all of the Reader threads came back to running and finished.
Note that I also tried the sleep(1000L) call in the version where Calculator is a Thread, and it did not make a difference, the notify() call was not required.
BTW, here is the same code, with the sleep() and some extra debugging info in place, and hopefully better-formatted:

So: can anyone answer the remaining question: Why is it different to wait() on a Thread object vs. a non-Thread object? Even when a Thread completes its run() method and "dies", isn't it still a valid Object? Is there a notifyAll() buried somewhere in the Thread code in one of the lifecycle methods that runs after run()? (I looked in the source for Thread, ThreadGroup, ThreadLocal, ThreadDeath, etc., but couldn't find it.)
-- Jon
Prabath Siriwardena
Greenhorn

Joined: Mar 27, 2004
Posts: 15
Dear Friend..!
A found a solution to the above , my own problem.
If you also had the same problem, elive this woud be useful.
Solution :
ThreadA, ThreadB and ThreadC are Reader Threads.
CalculatorThread and CalThread are Calculator Threads.
Case I
 ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
 CalculatorThread has a notify() call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
 CalculatorThread is still alive (not dead) when ThreaA,ThreadB,ThreadC hit their wait() call.

1) Only one Reader thread will be notified.(say ThreadB)
2) It will resume operations as soon as CalculatorThread notified and it exits the synchronized block, where “notify()” resides.
3) Other Reader threads will wait(). But now their wait() method behaves as the yield() method. ( Exactly as yield. It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)
4) So as soon as CalculatorThread exits it’s run() method, ThreadA & ThreadC resume their operaions.

Case I I
 ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
 CalculatorThread has a notify() ( or may not have a notify()) call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
 CalculatorThread is not alive (dead) when ThreaA,ThreadB,ThreadC hit their wait() call.
1) NO Reader thread will be notified.
2) All reader threads will wait(). But now their wait() method behaves as the sleep() method. ( Exactly as sleep(). It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)

Case III
 ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
 CalculatorThread has a notify() ( or may not have a notify()) call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
 CalculatorThread is not alive (dead) when ThreaA,ThreadB,ThreadC hit their wait() call.
 But CalThread is alive, and contains a notify() ( or may not have a notify())call inside it’s run() just after the expected task of Reader threads completed.
1) NO Reader thread will be notified, because none of them waits on CalThread object.
2) All reader threads will wait(). But now their wait() method behaves as the sleep() method. ( Exactly as sleep(). It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)
Case I V
 ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
 CalculatorThread has a notifyAll() call inside it’s run() just after the expected task of Reader threads completed. But Calculator thread can run beyond that.
 CalculatorThread is still alive (not dead) when ThreaA,ThreadB,ThreadC hit their wait() call.

1) All Reader threads will be notified.
2) All will resume operations as soon as CalculatorThread notified and it exits the synchronized block, where “notifyAll()” resides.
Case V
 ThreadA, ThreadB, ThreadC all wait on CalculatorThread to finish a certain task.
 CalculatorThread has NO notify() or notifyAll() calls inside it’s run().
 CalculatorThread is still alive (not dead) when ThreaA,ThreadB,ThreadC hit their wait() call.

1)No Reader thread will be notified.
2)All Reader threads will wait(). But now their wait() method behaves as the yield() method. ( Exactly as yield. It won’t release any locks it has now. But remember it has already released the lock on CalculatorThreard object, at the time it hit the wait() call)
3)So as soon as CalculatorThread exits it’s run() method, all Reader threads resume their operations.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Another victim
Thread has the undocumented behavior of callng notifyAll() when it dies.
 
 
subject: wait() without notify() or notifyAll()