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)

Thread pools 


What are thread pools? Why do we need these? Let's take a real-life example, consider a bus terminal that has a certain number of buses in the pool. The buses are added once to the pool and then are reused as required to serve various routes.   

Why does the bus terminal work this way? Why don't they buy buses as needed and discard (sell them) as per their demand? To buy a bus, you need to shell out money. There is an additional cost for maintaining them!  

So, the designers take a call, take into account the average travelers using the service, arrive at a certain number of buses, and reuse them as much as possible to maximize the return on investment:

Drawing a parallel, threads are expensive to create. So, we need to limit the number of threads in an application. Every thread has a stack of its own, which takes up memory. Each Java thread maps to an operating system thread. Thus, creation involves a system call, which is expensive. See this stack overflow link for more information...