Print in Order - Problem
Thread Synchronization Challenge: Print in Order
Imagine you're conducting an orchestra where three musicians need to play their parts in perfect sequence, but they can't see or hear each other! This classic concurrency problem asks you to coordinate multiple threads to execute methods in a specific order.
You're given a class
The challenge: Three different threads will call these methods simultaneously, but you must ensure they execute in the correct order: first โ second โ third. The operating system scheduler is unpredictable, so without proper synchronization, the output could be jumbled!
Goal: Design a thread-safe mechanism to guarantee sequential execution regardless of thread scheduling.
Imagine you're conducting an orchestra where three musicians need to play their parts in perfect sequence, but they can't see or hear each other! This classic concurrency problem asks you to coordinate multiple threads to execute methods in a specific order.
You're given a class
Foo with three methods:first()- prints "first"second()- prints "second"third()- prints "third"
The challenge: Three different threads will call these methods simultaneously, but you must ensure they execute in the correct order: first โ second โ third. The operating system scheduler is unpredictable, so without proper synchronization, the output could be jumbled!
Goal: Design a thread-safe mechanism to guarantee sequential execution regardless of thread scheduling.
Input & Output
example_1.py โ Basic Execution Order
$
Input:
Thread execution order: [1,2,3]
โบ
Output:
"firstsecondthird"
๐ก Note:
Even though threads start in order 1โ2โ3, the synchronization mechanism ensures they execute their methods in the correct sequence regardless of timing.
example_2.py โ Reverse Thread Start
$
Input:
Thread execution order: [3,1,2]
โบ
Output:
"firstsecondthird"
๐ก Note:
Despite thread 3 starting first, it must wait for threads 1 and 2 to complete their work. The synchronization ensures proper ordering.
example_3.py โ Random Thread Timing
$
Input:
Thread execution order: [2,3,1]
โบ
Output:
"firstsecondthird"
๐ก Note:
No matter how the OS schedules these threads, the output is always consistent thanks to our synchronization mechanism.
Visualization
Tap to expand
Understanding the Visualization
1
All chefs start cooking
Three chefs begin preparation simultaneously, but main course and dessert chefs wait for signals
2
Appetizer ready!
Appetizer chef finishes and rings the bell, signaling the main course chef to serve
3
Main course served
Main course chef serves their dish and signals the dessert chef
4
Perfect sequence complete
Dessert chef receives the signal and serves the final course - meal served in perfect order!
Key Takeaway
๐ฏ Key Insight: Use synchronization primitives like semaphores to coordinate threads efficiently - no busy waiting, just clean signals when each step is complete!
Time & Space Complexity
Time Complexity
O(1)
Each method executes exactly once with minimal blocking overhead
โ Linear Growth
Space Complexity
O(1)
Only requires a few semaphore objects for coordination
โ Linear Space
Constraints
- Exactly three threads will be created
- Each thread calls exactly one method on the same Foo instance
- Thread scheduling order is unpredictable and not guaranteed
-
Methods must execute in exact order:
first() โ second() โ third() - Solution must work regardless of OS thread scheduling
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code