• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is notify() mandatory?

 
Loganathan Karunakaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, I have a doubt related to the wait() and notify(). Please look at the following code:

1. class ThreadA {
2. public static void main(String [] args) {
3. ThreadB b = new ThreadB();
4. b.start();
5.
6. synchronized(b) {
7. try {
8. System.out.println("Waiting for b to complete...");
9. b.wait();
10. System.out.println("After waiting");
11. } catch (InterruptedException e) {}
12. System.out.println("Total is: " + b.total);
13. }
14. }
15. }
16.
17. class ThreadB extends Thread {
18. int total;
19.
20. public void run() {
21. synchronized(this) {
22. for(int i=0;i<100;i++) {
23. total += i;
24. }
25. notify();
26. }
27. }
28. }

In line number no 25, the notify() method is included. If I remove this notify(), will the thread that invokes the wait() method will wait for ever??.

I tried removing the notify() method. I get the same results irrespective of the presence of notify() method.

Output(Irrespective of whether I include notify() or not:

Wating for b to complete
0
1
3
6
10
15
21
28
36
45
After waiting
Please explain whether the notify() method is mandatory if we call the wait() method or once the called thread is completed, will the waiting method resume by itself..

Thanks
Loga
[ November 07, 2008: Message edited by: Loganathan Karunakaran ]
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are seeing is an implementation detail of the join() method. The join() method checks the status of the thread, and if the thread is still alive, it will call wait on the thread object. For this to work, there is a notifyAll() call in the thread's cleanup code -- meaning once the run() method terminates, during the cleanup, all the joining threads will be woken up, as soon as the thread is marked as no longer alive.

Now... It is unfortunate that the designers of the thread class, chose to use the thread class for this notification. I guess they didn't think that developers would like to wait on the thread instance for other reasons too. But regardless, this is an implementation detail. It may change in future implementation -- do not depend on notifications being sent upon thread termination.

Henry
 
Loganathan Karunakaran
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, Thanks for taking time to explain this.

Thanks much.

Regards
Loga
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believed they defended this on the grounds it was an example of 'spurious thread wakeup' (I'm not commenting on the argument)
ie wait should always be used in a loop testing the reason for wakeup as wait said it could just wake up without a notify.

So I'd put the wait in a loop as other reasons for spurious thread wakeup exist and new ones do appear.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!