Java Concurrency Interview

Synchronizers: latches, barriers, semaphores question 50 of 180 senior

What is an Exchanger for?

Exchanger<T> is a two-party meeting point: each thread calls exchange(myObject), blocks, and receives what the other one passed. It is a SynchronousQueue that goes both ways.

The canonical use is double buffering in a producer/consumer pair. The producer fills a buffer while the consumer drains another, and when both are done they swap buffers at the exchanger, with no allocation and no copying.

It is rare in application code, and that is fine to say. What it demonstrates in an interview is that you know the java.util.concurrent catalogue rather than just the three classes everyone names.

synchronizers exchanger

Quiz yourself on Synchronizers: latches, barriers, semaphores Suggest a correction