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

Sun tutorial Producer/Consumer

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Hello Friends
Please can you explain why the way to implement thread coordination with just flags wont work.
I cannot understand how the example is explained.
SUN tutorial LinK

Let's investigate how the code in CubbyHole's put and get methods helps the Producer and the Consumer coordinate their activities. The CubbyHole stores its value in a private member variable called contents. CubbyHole has another private member variable, available, that is a boolean. The available variable is true when the value has been put but not yet gotten and is false when the value has been gotten but not yet put. Here's one possible implementation for the put and get methods:
public synchronized int get() { //won't work!
if (available == true) {
available = false;
return contents;
}
}
public synchronized void put(int value) { //won't work!
if (available == false) {
available = true;
contents = value;
}
}

I CANNOT UNDERSTAND THE BELOW explantion for the above example?
As implemented, these two methods won't work. Look at the get method. What happens if the Producer hasn't put anything in the CubbyHole and available isn't true? The get method does nothing. Similarly, if the Producer calls put before the Consumer got the value, put doesn't do anything

Thanks
Farouk


SCJP, SCWCD, SCBCD, SCEA 5
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
First, this shouldn't even compile:



Second, look at their premise:

You really want the Consumer to wait until the Producer puts something in the CubbyHole and the Producer to notify the Consumer when it's done so. Similarly, the Producer should wait until the Consumer takes a value (and notifies the Producer of its activities) before replacing it with a new value. The two threads must coordinate more fully and can use Object's wait and notifyAll methods to do so.


This is a matter of design. They want put() and take() to wait on each other if nothing is currently available rather than returning immediately with nothing useful. Consequently they must use wait() and notifyAll() to prevent the thread from leaving either method before they can do their work.

If you didn't care about waiting you could change the first method to return -1 if nothing was available ans specify it in the API. I don't know why you'd ever want to do that though.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I'd also like to kick whoever wrote that tutorial for using (available == true) instead of (available) in their if-else blocks.
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Thanks Ken
So basically wait, notify and notify all is a matter of convienience from the developer point of view to improve performance by making other threads not checking the status of locks till they have been notified by the lock holding thread.

Am I right?
In my case this locking contention by other threads can happen only once during the whole day like (5.00 in the morning) as standing data will be refreshed once.

Critical Sections:
isRefreshRequired(Consumer)
RefreshCache(Producer).

I did not bother about wait , notify and notify all. Now Ideally I just have to sync both the above methods, as this is code is present as a part of every request i just have sync on producer method and let consumer call producer more than once based on the number of thread concurrency at the time but i dont want all threads to run producer at the same time.
Should be fine I gues your opinion?

Regards
Farouk
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
So basically wait, notify and notify all is a matter of convienience from the developer point of view to improve performance by making other threads not checking the status of locks till they have been notified by the lock holding thread.

Am I right?


No.

When you invoke wait() on an Object you are halting the current thread of execution until another thread invokes notify() or notifyAll() on the Object you invoked wait() on.

What this does is allow a developer to halt a thread until some condition is met. In the Producer/Consumer example the Consumer would check for the availability of what it wanted, if it was not available it would invoke wait() and consequently halt it's execution. Once that thread starts up again, either because another thread invoked notify() or notifyAll() on the same Object it invoked wait() on, or because it was interrupted, it then checks for availability again. If it's still not available, it waits once again. If it is available, it gets what it wanted and continues on it's merry way returning from that method.

The Producer does the same thing, but rather than waiting for something to be available to get it waits until there's nothing available to get and creates one so that one will be available.

In my case this locking contention by other threads can happen only once during the whole day like (5.00 in the morning) as standing data will be refreshed once.

Critical Sections:
isRefreshRequired(Consumer)
RefreshCache(Producer).

I did not bother about wait, notify and notify all. Now Ideally I just have to sync both the above methods, as this is code is present as a part of every request i just have sync on producer method and let consumer call producer more than once based on the number of thread concurrency at the time but i dont want all threads to run producer at the same time.
Should be fine I gues your opinion?

Regards
Farouk


I'm sorry, I don't understand exactly what you're doing. As I understand it you don't want more than one thread to try and refresh your data at the same time. If the code checking if a refresh is required and the code doing the actual refreshing is synchronized you should be fine. However, what's to prevent a consumer from getting corrupted data in the middle of a refresh? They all need to be synchronized. If your implementation is such that a thread is unable to get data while the data is being refreshed then any thread that tries to is effectively already waiting until it's done anyway.



That would be a case where it would work fine. Though it should be noted that if data is mutable it could be changed by other threads concurrently. But that's beyond the scope of the question I think.
[ November 29, 2005: Message edited by: Ken Blair ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I also forgot to mention that notify() only wakes one thread that's waiting on an Object rather than all of them. It's good to use when you know you'll only have one thread waiting at a time or if you don't care which thread gets woken.
 
Consider Paul's rocket mass heater.
 
subject: Sun tutorial Producer/Consumer