Imagine you're conducting a synchronized orchestra of threads! ๐ผ
You have a function printNumber that prints integers to the console. Your task is to implement a ZeroEvenOdd class that coordinates three different threads to produce a specific pattern.
The Challenge: Three threads must work together:
- Thread A: Calls
zero()- outputs only zeros - Thread B: Calls
even()- outputs only even numbers - Thread C: Calls
odd()- outputs only odd numbers
The threads must coordinate to produce the sequence: "010203040506..."
Pattern Breakdown: For n=3, output should be 010203 (length 2n=6)
- Position 1,3,5: zeros (0)
- Position 2: odd number (1)
- Position 4: even number (2)
- Position 6: odd number (3)
Key Challenge: Ensure proper synchronization so threads don't interfere with each other!
Input & Output
Visualization
Time & Space Complexity
Each number is printed exactly once, with efficient waiting
Only using semaphores and basic counters
Constraints
- 1 โค n โค 1000
- Three different threads will call zero(), even(), and odd() methods
- Output must be exactly 2n characters long
- Pattern must strictly alternate: 0 โ number โ 0 โ number โ ...