Core Java · Chapter 48 of 48
Interview: concurrency
By the end of this chapter you can fix the classic concurrency bugs (a deadlock, a racing singleton, a stop signal nobody sees) and hand work between threads safely. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The interview
- 5 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
The last round. At 6 am CSMT runs four tills, a kitchen and a stock sync at once, and Dev's worst bugs came from that hour: two stock transfers that froze the counter, a till registry loaded twice, a stop button the serving thread never noticed. So he hands you the broken code.
Chapters 38 to 40 gave you threads, atomics and pools. Each bug here compiles, passes a test with one thread, and fails only when two threads meet at the wrong moment, so you have to reason about it.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1 · interview question
Interview: StationStock.java, fix the deadlock in stock transfers with lock ordering
In StationStock.java, fix transfer(from, to, qty) so that two transfers in opposite directions at the same moment can never deadlock. It still moves qty cups from from to to while holding both stations' locks, but it always takes them in the same order, whatever the direction: first the station whose code comes first alphabetically, then the other. Take and release each lock with the station's own lock() and unlock() methods, and release both even when something throws. When from has less than qty, throw IllegalStateException and move nothing; when from and to are the same station, throw IllegalArgumentException.
For example, transfer(sbc, ndls, 5) and transfer(ndls, sbc, 5) both lock NDLS first, then SBC, and with 100 cups each both stations end at 100. transfer(sbc, ndls, 500) throws IllegalStateException and SBC keeps 100. Two threads doing 300 transfers each between CSMT and SBC, in opposite directions at once, both finish with no error and leave 1000 cups at each.
- Task 2 · interview question
Interview: OrderQueue.java, pass orders from a producer to a consumer through a BlockingQueue
In OrderQueue.java, make serve(orders, queue, cook) start two threads of its own and wait for both to finish. The counter thread puts every order into the queue with put, in order, and then puts DONE. The cook thread takes from the queue with take, applies cook to each order and collects the results, until it takes DONE (which is not cooked). Return the cooked results in the order of the orders. Use the queue you are given: it may be small, so the two threads must run at the same time, and nothing may be left in it at the end.
For example, 40 orders through a queue that holds 2, with a cook that adds hot in front, return [hot order 1, hot order 2, ..., hot order 40]: 41 puts, 41 takes, none of the cooking on the calling thread, and the queue empty at the end.
- Task 3 · interview question
Interview: TillRegistry.java, make a lazy singleton that is safe across threads
In TillRegistry.java, make getInstance() return the one TillRegistry, built the first time getInstance() is called and never before (not when the class is loaded, not when created() is called), and never twice, even when four threads call it at the same moment. Every caller gets the same object.
For example, created() is 0 before the first call; after four threads call getInstance() at once, created() is 1, all four got the same registry, and its tills are [SBC-1, SBC-2, NDLS-1, CSMT-1].
- Task 4 · interview question
Interview: ShiftGate.java, wait for the opening checks with a CountDownLatch
In ShiftGate.java, make openWhenReady(checks, timeout) run every check on a pool of up to 3 threads that it makes itself (never on the calling thread), and wait with a CountDownLatch until all of them have finished, but no longer than timeout. A check that throws still counts as finished. Return true when every check finished in time and false when the time ran out, after waiting the full timeout. With no checks, return true at once. Shut the pool down before returning without waiting for a check that is still running.
For example, four quick checks, one of which throws IllegalStateException, with a timeout of 2 seconds return true and all four ran. A quick check and one that hangs for 3 seconds, with a timeout of 200 ms, return false after about 200 ms.
- Task 5 · interview question
Interview: ShiftGate.java, make the stop signal visible to another thread
In ShiftGate.java, change how the closed field is declared so that serveUntilClosed(), running on another thread, is guaranteed to see close() and return. Mark it volatile and keep it a private boolean; close() and serveUntilClosed() stay as they are.
For example, a thread running serveUntilClosed() stops within 2 seconds after close() is called from the test's thread.
What you will learn
- Lock ordering: why two locks can deadlock, and the rule that prevents it.
- BlockingQueue: a producer and a consumer that wait for each other.
- Safe lazy initialisation: exactly one instance, even under a race.
- CountDownLatch and volatile: waiting for a set of jobs with a time limit, and making one thread's write visible to another.
More in The interview
Build "Interview: concurrency" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.