• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

blocking and waiting

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now, in this code, I am a bit confused that how will the reader threads reach the statement .......... c.wait() ?

This is because the Calculator Thread has started and it has got a lock on its own object. So while it is doing the calculations, Reader threads will not be even get the lock on the calculator object
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any response please ? This was an example from Kathy's certification book. So I am inclined to believe that this is correct but not getting it how ?
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure there must be some simple answer.. or do I need to re-phrase my question ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find the answer in the Java API documentation of method wait() in class Object - look it up.

You can only call wait() when the current thread owns the monitor of the object on which you call wait(). As the API documentation says, inside wait() the monitor will be temporarily released, and when wait() returns it is obtained again.

So while the thread is waiting inside the wait() method, it has released the lock on the object.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arnb, you are correct that if the Calculator runs first and acquires the lock, then none of the Readers will be able to acquire that lock until after the Calculator finishes. Which will create some problems, as discussed in the very next section of the book (assuming you've got the most recent edition, for SCJP 1.5).

Note that as far as I'm aware, both the 1.4 and 1.5 versions of this book have a different main() method than you showed:

The calculator is started after the readers, which makes it more likely that the readers will acquire locks and wait() before the calculator. It's not guaranteed, however. Which is why they wait() should really be inside a loop which checks some condition, as discussed in the next section of the book. The code sample shown is not intended to be perfect; it demonstrates some characteristics of threaded code, but not all of them. Continue reading to learn more.
[ July 27, 2007: Message edited by: Jim Yingst ]
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Jim... that is exactly what I thought... Calculator thread should start after the reader threads have started... that gives me better confidence about my understanding ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic