• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

problem with wait()- notify()

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Henry & Neha . I got it now
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created a new topic with this question
https://coderanch.com/t/480381/Programmer-Certification-SCJP/certification/IllegalMonitorStateException-from-notify#2155023

Joss Armstrong
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic