DEV Community

Oluwanifemi Latunde
Oluwanifemi Latunde

Posted on

Fizz Buzz Multithreaded

Day 6 of the #I4G10DaysOfCodeChallenge, and it is the multi-threaded variant of the well-known fizz buzz problem on Leetcode.

We want the four different threads—fizz, buzz, fizzbuzz, and number—to create the output in the proper order. Each thread will call one of the four functions—fizz, buzz, fizzbuzz, and number—exactly once.

I used the locking method to solve this problem.

Despite the fact that there are four separate loops, only one will ever increase the shared counter variable because of the mutually exclusive if conditions. They never stop spinning until it's "their turn" to print an output and increase the value of the counter.

Oldest comments (0)