Synchronizers
16 questions in 5 subtopics.
CountDownLatch
Write code that starts 5 worker tasks and waits until all have finished their setup step before the main thread continues, with a timeout of 30 seconds. Explain what happens if a worker throws before counting down.An integration test that waits onlatch.await() sometimes hangs the whole CI build. The latch is counted down from inside a message listener. What are the likely causes and how would you make the test robust?When would you use CountDownLatch rather than Thread.join, ExecutorService.invokeAll or CompletableFuture.allOf to wait for work?Services in your platform use CountDownLatches to block startup until caches are warm and connections are ready, and some hang forever in Kubernetes when a dependency is down. What standard for startup coordination would you set?Teams increasingly try to coordinate work across many processes with tools that resemble in-process synchronizers, such as distributed latches and barriers built on Redis or ZooKeeper. What long-term guidance would you give about coordinating work across processes?CyclicBarrier
How doesCyclicBarrier differ from CountDownLatch, and what happens when one waiting thread is interrupted or times out?A simulation runs 8 worker threads that each compute one slice of a grid per step and wait at a CyclicBarrier before the next step. If one worker hits an exception, the others stop with BrokenBarrierException and the run is lost. How would you handle failures and recovery?Semaphore
Limit concurrent calls to a fragile downstream service to 10 using a Semaphore, and point out two common bugs with semaphores.After switching a Spring Boot service to virtual threads, a downstream inventory service is overloaded because the number of concurrent calls is no longer limited by the Tomcat thread pool. How would you limit it?Define how services should isolate calls to their dependencies: semaphore-based bulkheads or thread-pool bulkheads. Consider virtual threads, timeouts and observability.Concurrency limits moved from thread pool sizes to semaphores and now to adaptive limits that adjust from observed latency. What long-term stance would you take on static versus adaptive concurrency limits for your services?Phaser
What does Phaser offer beyondCountDownLatch and CyclicBarrier, and how do register, arriveAndAwaitAdvance and arriveAndDeregister work?A crawler starts with one seed task; each task may spawn more tasks, and the main thread must wait until all tasks finish. Describe how to use a Phaser for this.Most java.util.concurrent synchronizers, such as ReentrantLock, Semaphore, CountDownLatch and ReentrantReadWriteLock, are built on AbstractQueuedSynchronizer, while Phaser and Exchanger have their own implementations. What does this show about building a library of primitives, and how should platform teams structure their own?Exchanger
What doesjava.util.concurrent.Exchanger do, and describe a situation where it fits.In code review guidance for your teams, how would you describe when to use each of CountDownLatch, CyclicBarrier, Semaphore, Phaser and Exchanger, and which ones should prompt extra scrutiny?No question matches these choices.