Grokking Multithreading and Concurrency for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Problem 7: FizzBuzz Multithreading Problem
On this page

Overview

Step-by-step Algorithm

Algorithm Walkthrough

Key Points

Overview

The FizzBuzz problem has been around for a long time as a classic programming challenge. In this multithreaded variant, we are tasked to print a series from 1 up to a number n with some rules:

  • Print "fizz" for numbers divisible by 3.
  • Print "buzz" for numbers divisible by 5.
  • Print "fizzbuzz" for numbers divisible by both 3 and 5.
  • Print the number itself otherwise.

The twist here is to print this sequence concurrently using four separate threads, adding complexity in terms of synchronization.

Step-by-step Algorithm

  1. Initialize

    • Start with the first number.
    • Set up synchronization tools (locks, conditions, etc.).
  2. Thread A (Fizz Printer)

    • While the current number is less than or equal to n
      • Acquire the lock.
      • Wait until the current number is divisible by 3 but not by 5.
      • Print "fizz".
      • Increment the current number.
      • Notify all other threads.
      • Release the lock.
  3. Thread B (Buzz Printer)

    • Similar to Thread A, but the condition changes:
      • Wait until the current number is divisible by 5 but not by 3.
      • Print "buzz".
  4. Thread C (FizzBuzz Printer)

    • A combination of A & B:
      • Wait until the current number is divisible by both 3 and 5.
      • Print "fizzbuzz".
  5. Thread D (Number Printer)

    • Waits for numbers that are neither divisible by 3 nor 5:
      • Wait until the current number is not divisible by either 3 or 5.
      • Print the current number.
  6. Wrap-up

    • When the current number exceeds n, all threads stop.

Algorithm Walkthrough

Let's take a small value of n, say n = 15, and see how threads interact:

  1. Initialization

    • current_number starts at 1.
    • A lock is initialized to enforce mutual exclusion.
    • Condition variables for each thread's condition are set up to control the flow of execution.
  2. Threads Execution

    • Thread A (Fizz Printer): Waits for current_number to be divisible by 3 but not by 5. When it's its turn, it acquires the lock, prints "Fizz", increments current_number, notifies other threads, and releases the lock.
    • Thread B (Buzz Printer): Does the same as Thread A, but for numbers divisible by 5 and not by 3.
    • Thread C (FizzBuzz Printer): Waits for current_number to be divisible by both 3 and 5. It follows the same synchronization pattern.
    • Thread D (Number Printer): Waits for numbers that are divisible by neither 3 nor 5 and prints the number itself.
  3. Synchronization in Action

    • When current_number is 1, only Thread D can print because it's neither divisible by 3 nor 5. Threads A, B, and C are waiting.
    • When current_number is 3, Thread A wakes up as the condition is satisfied, prints "Fizz", and then increments current_number.
    • At current_number = 5, Thread B wakes up to print "Buzz".
    • This pattern continues, with each thread waiting for its correct turn based on the value of current_number.
  4. Wrap-up

    • When current_number exceeds n, no more printing occurs, and all threads will eventually stop after completing their last check.

Key Points

  • Locks are used to prevent more than one thread from entering a critical section of code at the same time.
  • Condition Variables allow threads to wait for a certain condition to become true. In this case, the conditions are based on the divisibility of current_number.
  • Each thread is responsible for waiting for its specific condition to be true before it can execute its print function.
  • Threads notify each other after printing so the next correct thread can take its turn.
  • Proper synchronization ensures the sequence of numbers is printed according to the FizzBuzz rules, even though the work is divided across multiple threads.

Here are various implementations:

mediaLink

1 of 6

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page

Overview

Step-by-step Algorithm

Algorithm Walkthrough

Key Points