Sunday, April 13, 2014

Five Synchronizers in Java

Semaphore CountDownLatch CyclicBarrier Phaser Exchanger
You already understand the low-level concurrency constructs (such as the use of the synchronized keyword, Runnable interface, and Thread class for creating threads). In the case of a shared resource that needs to be accessed by multiple threads, access and modifications to the shared resource need to be protected.

When you use the synchronized keyword, you employ mutexes to synchronize between threads for safe shared access. Threads also often needed to coordinate their executions to complete a bigger higher-level task. The wait/ notify pattern is one way to coordinate the execution of multiple threads.

Using APIs for acquiring and releasing locks (using mutexes) or invoking the wait/notify methods on locks are low-level tasks. It is possible to build higher-level abstractions for thread synchronization. These high-level abstractions for synchronizing activities of two or more threads are known as synchronizers. Synchronizers internally make use of the existing low-level APIs for thread coordination.

Five classes help common special-purpose synchronization idioms.

  1. Semaphore is a classic concurrency tool.
    Semaphores are often used to restrict the number of threads than can access some (physical or logical) resource.
  2. CountDownLatch is a very simple yet very common utility for blocking until a given number of signals, events, or conditions hold.
    A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes
  3. A CyclicBarrier is a resettable multiway synchronization point useful in some styles of parallel programming.
    A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released.
  4. A Phaser provides a more flexible form of barrier that may be used to control phased computation among multiple threads.
    A reusable synchronization barrier, similar in functionality to CyclicBarrier and CountDownLatch but supporting more flexible usage.
  5. An Exchanger allows two threads to exchange objects at a rendezvous point, and is useful in several pipeline designs.
    A synchronization point at which threads can pair and swap elements within pairs. Each thread presents some object on entry to the exchange method, matches with a partner thread, and receives its partner's object on return. An Exchanger may be viewed as a bidirectional form of a SynchronousQueue. Exchangers may be useful in applications such as genetic algorithms and pipeline designs.

If you know anyone who has started learning Java, why not help them out! Just share this post with them. 
Thanks for studying today!...


  1. It is really interesting... Is there any books you could refer for reading such concepts further?

    1. Java Concurrency in Practice
      Programming Concurrency on the JVM