0% completed
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
-
Initialize
- Start with the first number.
- Set up synchronization tools (locks, conditions, etc.).
-
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.
- While the current number is less than or equal to n
-
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".
- Similar to Thread A, but the condition changes:
-
Thread C (FizzBuzz Printer)
- A combination of A & B:
- Wait until the current number is divisible by both 3 and 5.
- Print "fizzbuzz".
- A combination of A & B:
-
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.
- Waits for numbers that are neither divisible by 3 nor 5:
-
Wrap-up
- When the current number exceeds
n
, all threads stop.
- When the current number exceeds
Algorithm Walkthrough
Let's take a small value of n
, say n = 15
, and see how threads interact:
-
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.
-
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", incrementscurrent_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.
- Thread A (Fizz Printer): Waits for
-
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 incrementscurrent_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
.
- When
-
Wrap-up
- When
current_number
exceedsn
, no more printing occurs, and all threads will eventually stop after completing their last check.
- When
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:
1 of 6
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible