Coordinate Threads in Order
🧵 What is thread ordering? Several threads share one object, and the operating system starts and pauses them whenever it likes. Thread ordering means adding just enough coordination that their output still comes out in a fixed order: first before second, foo before bar, a zero before every number. Each thread does its own small job, but only when it is its turn.
Interviewers ask this in many costumes: print in order, print FooBar alternately, print zero-even-odd, build H2O molecules, and FizzBuzz with four threads. They look like five problems, but they are one design. Each needs a small piece of shared state that says whose turn it is, a lock that protects it, and a way for a thread to sleep until the state says "you". Learn that recipe once and each puzzle becomes a matter of choosing the right state. We build the recipe on the first puzzle and then apply it to the other four.
Requirements
"Three threads call
first(),second()andthird()on the same object. Make sure the output is always first, second, third. Then: what if two threads must alternate? What about four?"
Clarifying Questions
You: Can the threads start in any order?
Interviewer: Yes. Assume the worst: the thread that should go last may start first and run as fast as it can.
You: Is it fine for a waiting thread to loop and check a flag?
Interviewer: No. Waiting threads must sleep, not spin. We run these on busy machines.
You: Which tools can I use? Locks, condition variables, semaphores?
Interviewer: Any standard primitive in your language. No sleeping for a fixed time and hoping.
You: Does each thread call its method once, or in a loop?
Interviewer: It depends on the puzzle. In print-in-order each thread calls once. In FooBar and the others, each method loops until its share of the output is done.
You: For the water puzzle, does the order inside a molecule matter?
Interviewer: No. Any three releases in a row must be two hydrogens and one oxygen, in any order.
You: When all the output is printed, do the methods have to return?
Interviewer: Yes. A thread that never finishes is a bug, even if the output looks right.
The second answer rules out the first idea most people have, a while not flag: pass loop. The last answer matters for FizzBuzz: three of the four threads have nothing left to do near the end, and they must notice and stop.
💡 Interview tip: Say "the threads may start in any order" out loud before you write anything. Every bug in these puzzles is a thread arriving earlier than you pictured. Naming the worst case first keeps you from writing code that only works when threads start politely.
Final Requirements
- Print in order:
first,secondandthirdrun in that order, whatever order their threads start in. - FooBar: two threads print
fooandbaralternately,ntimes each, starting withfoo. - Zero-even-odd: three threads print
0 1 0 2 ... 0 n. One prints the zeros, one the odd numbers, one the even numbers. - H2O: one thread per atom. Every three releases in a row are two hydrogens and one oxygen.
- FizzBuzz: four threads print
1..nin order, asfizz,buzz,fizzbuzzor the number. Every thread returns when the sequence is done. - A waiting thread blocks without using CPU, and every method returns once its work is done.
Out of scope
- Fairness between threads and timing. We only care about the order of the output.
- Interrupting or cancelling a thread halfway.
- Making the printing itself fast. Each callback is a quick print.