• 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

wait() notify() Mailbox (Heller's threads)

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taken from Heller et al's 4th ed (modified).

I have a thread (which loops) that does a wait() for a Mailbox object to have a message associated with it. When a message is there, it spews the message to the console.

The user running is prompted (in a loop in the main thread) to enter a message and press Enter. Once he presses Enter, a storeMessage() method is run which does a notify();

I was trying to hand code the Mailbox example using wait() and notify() I found that I got the following output:


Question: I can see what's missing in the code (because I kinda copied Heller's example and just commented out the stuff I forgot), but what exactly does the commented out stuff do?

btw, I added the sleep() in the retrieveMessage() because I had some intuition that something might not work, and this output proves it.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic