File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with wait()- notify()

 
saima kanwal
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code is from K & B Page 748 ( a little bit modified). I was trying to understand the concept of threads :



In the above code if i delete notify() from ThreadB class , i get the same output as the previous one .



and the output in both cases is
total = 45

According to me , the second code without notify() should add the ints but should not print the total and wait for ever because there is no notify(). But the code prints the total , just like the previous code which has notify();

can anyone explain this to me , what 's happening here ?
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saima kanwal wrote:
can anyone explain this to me , what 's happening here ?


Basically, you are encountering an implementation detail. The join() mechanism is using the thread object for notification. Threads that are waiting to join() call the wait() method on the thread object -- and when a thread terminates, one of the tasks that is done is a notifyAll().

This is an implementation detail, and can't be dependent to behave this way with all JVMs.

Henry
 
saima kanwal
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
saima kanwal wrote:
can anyone explain this to me , what 's happening here ?


Basically, you are encountering an implementation detail. The join() mechanism is using the thread object for notification. Threads that are waiting to join() call the wait() method on the thread object -- and when a thread terminates, one of the tasks that is done is a notifyAll().

This is an implementation detail, and can't be dependent to behave this way with all JVMs.

Henry


What do you mean by implementation detail?? i simply cannot understand your explanation. where has join() come in this code ?? Do you mean to say that the code should run in the manner I expected( should wait forever) but it is due to my particular JVM's behaviour?? or what ?? please explain.
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saima kanwal wrote:
What do you mean by implementation detail?? i simply cannot understand your explanation. where has join() come in this code ?? Do you mean to say that the code should run in the manner I expected( should wait forever) but it is due to my particular JVM's behaviour?? or what ?? please explain.


An "implementation detail" is a design decision made during implementation, and not during specification, yielding an effect which is not intended.

In this case, when Sun implemented the join() method they used the Thread object to signal that the thread has terminated. Because of this, any other thread, that waits on the thread object (and not for join()), will have an unintended effect of getting an notification too.


In other words, don't use the thread object for the wait() method. If you do, you will be sharing it with the join() mechanism.

Henry
 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lets see it this way Saima,
In first code, when you call notify main thread is notified and acquires lock, I think this you understood.
Now, in second code you did not make a call for notify but as soon as the run method completes the thread releases its lock which is now available to all the threads waiting for that lock. I think I read somewhere at Java Ranch that if there is no notify in the run method then after the run method a call to notifyAll is implicitly made.
 
saima kanwal
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Henry & Neha . I got it now
 
Joss Armstrong
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can someone explain to me why the following code gives an IllegalMonitorStateException when the notify method is called?

N.B. It works correctly when a public Object o is instantiated in the Example1 class and this is used to synchronize on.



Thanks

Joss Armstrong
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joss Armstrong wrote:Please can someone explain to me why the following code gives an IllegalMonitorStateException when the notify method is called?


Please don't hijack topics. Start a new topic for a new (different) question.

Henry
 
Joss Armstrong
Greenhorn
Posts: 17
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic