Scrivio Interview library

Synchronizers

16 questions in 5 subtopics.

CountDownLatch

CodingFoundations6 min read
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.
DebuggingFoundations6 min read
An integration test that waits on latch.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?
ConceptIntermediate6 min read
When would you use CountDownLatch rather than Thread.join, ExecutorService.invokeAll or CompletableFuture.allOf to wait for work?
ScenarioAdvanced7 min read
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?
LeadershipPrincipal7 min read
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

ConceptFoundations6 min read
How does CyclicBarrier differ from CountDownLatch, and what happens when one waiting thread is interrupted or times out?
ScenarioIntermediate7 min read
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

CodingFoundations6 min read
Limit concurrent calls to a fragile downstream service to 10 using a Semaphore, and point out two common bugs with semaphores.
ScenarioIntermediate6 min read
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?
DesignAdvanced6 min read
Define how services should isolate calls to their dependencies: semaphore-based bulkheads or thread-pool bulkheads. Consider virtual threads, timeouts and observability.
LeadershipPrincipal6 min read
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

ConceptIntermediate6 min read
What does Phaser offer beyond CountDownLatch and CyclicBarrier, and how do register, arriveAndAwaitAdvance and arriveAndDeregister work?
CodingIntermediate6 min read
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.
ConceptPrincipal7 min read
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

ConceptIntermediate6 min read
What does java.util.concurrent.Exchanger do, and describe a situation where it fits.
LeadershipAdvanced6 min read
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?