I wanted to test my understanding of thread synchronization because various online tutorials and APIs on Threads provided vague and even conflicting explanations. I developed the Washer/Dryer scenario to determine the right answers. The Requirements Each Washer and Dryer is a thread and must sleep until a dish appears in the rack to its left. If another Washer or Dryer grabs the dish, leaving none for this Washer or Dryer, it should go back to sleep. If a Washer has finished washing a dish, it can only put it in the clean rack if there is room. If there is no room, it must sleep (RED) while holding the dish until there is room to put it in the clean rack (once a Dryer removes a dish from that rack). The clean rack is shared by the Washers and Dryers: only one should be allowed to change the contents of the rack at any moment. Washer and Dryer activity must be coordinated.
|