• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Feed the Pets

 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan,
Should second method be not for cat?
[ October 07, 2002: Message edited by: Barkat Mardhani ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic