Synchronizers: latches, barriers, semaphores question 50 of 180 senior
What is an Exchanger for?
- Swapping the contents of two collections atomically
- Correct: A rendezvous where exactly two threads meet and swap objects, each blocking until the other arrives
- Exchanging a lock between two threads
- A queue with two ends
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.
Quiz yourself on Synchronizers: latches, barriers, semaphores Suggest a correction