Book Image

Concurrent Patterns and Best Practices

By : Atul S. Khot
Book Image

Concurrent Patterns and Best Practices

By: Atul S. Khot

Overview of this book

Selecting the correct concurrency architecture has a significant impact on the design and performance of your applications. Concurrent design patterns help you understand the different characteristics of parallel architecture to make your code faster and more efficient. This book will help Java developers take a hands-on approach to building scalable and distributed apps by following step-by-step explanations of essential concepts and practical examples. You’ll begin with basic concurrency concepts and delve into the patterns used for explicit locking, lock-free programming, futures, and actors. You’ll explore coding with multithreading design patterns, including master, slave, leader, follower, and map-reduce, and then move on to solve problems using synchronizer patterns. You'll even discover the rationale for these patterns in distributed and parallel applications, and understand how future composition, immutability, and the monadic flow help you create more robust code. By the end of the book, you’ll be able to use concurrent design patterns to build high performance applications confidently.
Table of Contents (14 chapters)

A cyclic barrier

A cyclic barrier is one more synchronization mechanism where all threads need to wait at a point before any can proceed. 

A real-life example should make it clear. Three long-time friends happen to be in the same city on business, and they plan to have dinner together, and then go and see a movie, just to relive old times.

One of them is going to take a car, and they happen to be the nearest to the restaurant. So, they arrive quickly and wait at the venue. The other buddies are taking a train and a bus, respectively, so everyone waits for the other(s) to arrive. 

Once everyone arrives, the dinner can begin. This agreement of waiting upon other buddies to join, and then only begin the fun, is a barrier:  

After the dinner is over, and as people eat at different rates, not everyone finishes at the same time. So, again, they wait (barrier), and then, once everyone is done, they go to the movie.   

A barrier is very much like a countdown latch, with the only difference being that...