Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Feed the Pets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Feed the Pets" Watch "Feed the Pets" New topic
Author

Feed the Pets

Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
I developed the following question for my thread exam, but I think that it might have become a little long. I'm not sure that I want to put it on my exam, but I thought that I would post it here to see how everyone feels about it

[ October 07, 2002: Message edited by: Dan Chisholm ]

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

Dan,
Should second method be not for cat?
[ October 07, 2002: Message edited by: Barkat Mardhani ]
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Barkat Mardhani:

Dan,
Should second method be not for cat?
[ October 07, 2002: Message edited by: Barkat Mardhani ]

Barkat,
Thank you for pointing that out. I made the correction in the earlier post so the cat is now happy.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Dan:
I could understand the general flow of the program but could not conceptulate the output (I guess my IQ is not that great). Could you point me to a good resource for threads that I can read lying down (I mean light reading)
Thanks
Barkat
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Dan, when I see a problem that long, I have to print it out. I forget what's up there when I'm at the bottom.
And I don't normally look at code that long unless I'm paid for it.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
It was intended to be a humorous example of the use of the wait() and notifyAll() methods but it grew to be far too long. The idea is that a person named George has two pets, a dog and a cat. Both of the pets become hungry as soon as they are started. After they notify their human that they are hungry they begin to wait for their food to made available. After the human is notified that his pets are hungry he then notifies both pets that food is available when in reality the food is not yet available. The dog wakes up and growls when he finds no food. The cat responds with a hiss--the feline equivalent of a growl. Next, the human makes the cat food available and then notifies both pets. This time, the cat is happy and starts to purr. The dog is once again angry and growls once more. Next, the human makes the dog food available and notifies both pets. The cat is no longer listening and does not respond. The dog responds with a happy bark.
O.K. maybe my thread humor isn't very good and the program is far too long. At least I had some fun writing it.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Barkat Mardhani:
Hi Dan:
I could understand the general flow of the program but could not conceptulate the output (I guess my IQ is not that great). Could you point me to a good resource for threads that I can read lying down (I mean light reading)
Thanks
Barkat

Here's some links.
http://java.sun.com/docs/books/tutorial/essential/threads/index.html
http://www.javaworld.com/javaworld/jw-05-2002/jw-0503-java101_p.html
http://www.artima.com/insidejvm/ed2/ch20ThreadSynchronization01.html
There's also books such as "Practical Java" and "Effective Java"
If I had learned about threads recently then I could provide a better list of resources. I learned about multi-tasking and multi-processing long ago while working on a real-time machine control application at a semiconductor equipment company. We were using two real-time operating systems--VRTX and VxWorks. The java Thread class is just an API to the underlying threading features of the operating system.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Dan. Nice example.
This piece of code arises interesting comments.

Generally it is a good idea to synchronize the whole "spin" lock under the same block:

otherwise there is a risk of slipped conditions: Though with the given code it is not possible, imagine another thread executes at line (1) or (2) and makes the tested condition to change from what is expected, the following wait() or notiyAll() excuted by the original thread will be relying on a false condition. This might be the cause, not in this example, for an object to become in an inconsistent state.
There is a tricky subtlety at line (3). The first time it is executed the corresponding access to the variable dogFoodReady must be from the main memory, but the next ones are not forced to be from main memory because the variable is not volatile neither its access occurs under synchronization. The following acceses could be from the working memory of the thread, where it might be not updated to the last value written by another thread. Line (4) solves this problem because the lock action flushes all the variables in the thread's working memory, thus next accesses will be from main memory. I think the dependency between lines (3) and (4) is tricky enough to consider that synchronizing the whole spin lock under the same block makes this point explicit.
What I mean is that although this code is not affected I think it is a good idead, in a multithreaded application, to fully synchronize the test of a condition and its use.


SCJP2. Please Indent your code using UBB Code
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Jose,
I rewrote the example to incorporate your suggestions. Also, I added some code that will force the Human to listen to the response of each pet. Do you have any suggestions for the following?


The above code prints the following.
Tease Pets
Angry Growl
Angry Hiss
Feed the Cat but tease the Dog.
Angry Growl
Happy Purr
Feed the Dog.
Happy Bark
[ October 10, 2002: Message edited by: Dan Chisholm ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
You can leave out

and the output is still the same because at the time main executes it the other two threads might be waiting in h.wait(); or not.
Just to state that all the "shared" fields are written only by one thread, thus the synchronization is not needed, here, in order to make this change visible to the reading thread --this can be acomplished just by volatile-- , but to obtain the intended flow of execution.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Jose,
Good point. Since I moved the output from the Dog and Cat classes to the Human class, invoking notifyAll no longer produces any output.
The attempt to tease both the Dog and Cat by invoking notifyAll without any food available has been removed. Now the cat gets fed before notifyAll is invoked.

The new output is as follows.
Feed the Cat but tease the Dog.
Angry Growl
Happy Purr
Feed the Dog.
Happy Bark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Feed the Pets
 
Similar Threads
logic for lock
synchronisation error
please critique the code
compile time errors in the program
wait without notify