Tech Interview RoomStudy guides

Advanced Java

Explain wait/notify, BlockingQueue and the main synchronizers (CountDownLatch, CyclicBarrier, Semaphore).

Advanced · 2 min read · Lesson 10 of 10 in Advanced Java

wait / notify

Low-level coordination on an object's monitor. wait() releases the lock and suspends the thread until notify()/notifyAll(); both must be called while holding the lock. Always wait in a loop that re-checks the condition. Modern code rarely needs them.

BlockingQueue

put() blocks when full and take() blocks when empty, so producer-consumer needs no explicit locking. ArrayBlockingQueue (bounded, one lock), LinkedBlockingQueue (separate put/take locks, optionally bounded), PriorityBlockingQueue, DelayQueue, SynchronousQueue (direct hand-off).

Synchronizers

  • CountDownLatch(n): threads await() until countDown() has been called n times. One-shot. Example: start serving only after all caches have warmed.
  • CyclicBarrier(n): n threads wait for each other at a barrier, then all proceed; reusable, with an optional barrier action. Example: phases of a parallel simulation.
  • Semaphore(permits): limits concurrent access. Example: allow at most 10 concurrent calls to a rate-limited partner API, which is the right way to bound concurrency with virtual threads.
  • Phaser: a flexible, reusable barrier with a dynamic number of parties.
  • Exchanger: two threads swap objects.

Example

java
Semaphore partnerLimit = new Semaphore(10);

Quote callPartner(Request r) throws InterruptedException {
    partnerLimit.acquire();
    try {
        return partnerClient.quote(r);
    } finally {
        partnerLimit.release();
    }
}

Follow-up questions interviewers ask

Try answering each one before you open it.

Why must wait() be called inside a while loop?

Because of spurious wakeups (the JVM may wake a thread without a notify), and because by the time a notified thread reacquires the lock, another thread may already have consumed the condition (with notifyAll, several consumers wake for one item). Re-checking the condition in a while loop is the only correct pattern.

CountDownLatch vs CyclicBarrier?

A latch is one-shot and separates the waiters from the counters: worker threads count down, and a different thread (the coordinator) awaits. A barrier is reusable and symmetric: the same n threads both arrive and wait, and all are released together, optionally after running a barrier action. If a thread waiting at a barrier is interrupted or times out, the barrier breaks and all waiters get BrokenBarrierException.

notify() vs notifyAll()?

notify() wakes one arbitrary waiter; if that waiter is waiting for a different condition (for example a producer woken when a consumer was needed), the signal is lost and threads can stall. notifyAll() wakes every waiter, and each re-checks its condition. It is safer but costs more context switches. With ReentrantLock, separate Condition objects (notFull, notEmpty) let you signal exactly the right group.