Understanding Multi-Threading by Real-Life Example — The Washing-Machine Pattern

Nava Polak Onik
4 min readFeb 15, 2021
Photo by Sebastian Herrmann on Unsplash

There are many multi-threading examples on the net teaching us how to implement producer-consumer model. I found out that taking these examples and implementing them in practice is not so obvious.

In the last cycle of job search, I got a home task including a multi-threading challenge which was a bit unusual; I had to run a long task, and interrupt it when notifications are received. Taking the real-life washing machine example makes it much easier to understand.

Code for this example can be found here.

The model

  1. We need to process some items.
  2. The processing time is long and is not related to the number of items been processed.
  3. We cannot add items during processing
  4. If we get during processing one or more items waiting to be processed, the processing will start over when finished.
  5. If no items are waiting it will stop till new items arrive.

Washing Machine Model

This is the exact functioning of a washing machine.

We add items to the machine. Processing time does not depend on the number of items and new items will wait till we operate the machine for an additional cycle.

Bonus: The washing machine has a max capacity. In case we want to add items above the machine capacity, we will have to wait for the next cycle until we can add these items to the machine.

For the simplicity of this model, let's assume that the machine will start working no matter how many items are waiting. It will start working for one or multiple items.

The implementation contains the following parts:

  1. The collection holding the data which may be any collection as queue/List.
  2. A consumer which is the washing machine
  3. Producer — Adding items to the laundry bag.

The code I’m going to demonstrate has 2 layers:

  • The abstract layer
  • The specific implementation for washing machine.

The idea is you can take the abstract code and make your own implementation for any other use.

Laundry Model with Producer Consumer Diagram

Here is the Washing machine implementation, Let’s focus on each part:

Data Storing — the Queue

Data can be a queue or any other collection. In this example, I will use a List since we may add single or multiple items at a time but all the items are consumed at the same time.

The queue is the most sensitive part to concurrent changes. We will use mutex (ReentrentLock) in order to protect the data upon changes and use semaphores in order to make sure items are consumed only when exists and items are added only when queue is below maximum capacity.

Add Items

In adding items we will use the producerSemaphore in order to be blocked till the list contains items up to the max capacity and we use the lock upon adding the item in order to avoid concurrent changes to the list.

Get All Items

This is a bit different from the classical consumer-producer example since the machine consumes all items at once. The same as in the add items, we use the lock in order to assure that during clearing the list (emptying the laundry bag), no items are added.

The Consumer

Abstract Implementation:

Washing Machine Specific Implementation:

The Producer

Actually, the only implementation of the producer is running it as a separate thread and adding items. Here is an example:

Running the Model

Each machine cycle is 5000 ms. According to the addition of items in the code, we expect:

  • item1 to run in the first cycle.
  • items 2–6 are added during cycle 1, so all of them will run in the second cycle
  • items 7–9 are run during cycle 3
  • In the forth cycle there are no items, process is interrupted and finish.

Here is the output:

Start adding items
Starting machine cycle number 1
Adding item item1 to laundry bag
Following items will be processed in cycle: 1
item1
Waiting 5000 for wash machine cycle number 1 to finish
Adding item item2 to laundry bag
Adding item item3 to laundry bag
Adding item item4 to laundry bag
Adding item item5 to laundry bag
Adding item item6 to laundry bag
Machine cycle number 1 wash finished
Starting machine cycle number 2
Following items will be processed in cycle: 2
item2
item3
item4
item5
item6
Adding item item7 to laundry bag
Waiting 5000 for wash machine cycle number 2 to finish
Adding item item8 to laundry bag
Adding item item9 to laundry bag
Machine cycle number 2 wash finished
Starting machine cycle number 3
Following items will be processed in cycle: 3
item7
item8
item9
Waiting 5000 for wash machine cycle number 3 to finish
Machine cycle number 3 wash finished
Starting machine cycle number 4
Process Finished
Going to close all
Machine cycle number 4 wash finished

--

--

Nava Polak Onik

Lots of experience as a Software Engineer. Love to share my knowledge with others.