File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization 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 » Threads and Synchronization
Bookmark "blocking and waiting" Watch "blocking and waiting" New topic

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

Joined: Jan 10, 2002
Posts: 63844

Please read this.

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

Joined: Aug 16, 2005
Posts: 15081

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 8 API documentation
Jim Yingst

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:
subject: blocking and waiting
It's not a secret anymore!