File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes blocking and waiting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "blocking and waiting" Watch "blocking and waiting" New topic
Author

blocking and waiting

Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145


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

Joined: Feb 23, 2004
Posts: 145
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

Joined: Feb 23, 2004
Posts: 145
I am sure there must be some simple answer.. or do I need to re-phrase my question ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Please read this.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
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 ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: blocking and waiting
 
Similar Threads
Question on Thread example in Kathy Sierra 1.4 book
blocking and waiting
Question regarding notify() and wait()
Threading discrepancies
synchronization doubt