Concurrent Patterns and Best Practices
eBook - ePub

Concurrent Patterns and Best Practices

Build scalable apps with patterns in multithreading, synchronization, and functional programming

  1. 264 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Concurrent Patterns and Best Practices

Build scalable apps with patterns in multithreading, synchronization, and functional programming

About this book

A definitive guide to mastering and implementing concurrency patterns in your applications

Key Features

  • Build scalable apps with patterns in multithreading, synchronization, and functional programming
  • Explore the parallel programming and multithreading techniques to make the code run faster
  • Efficiently use the techniques outlined to build reliable applications

Book Description

Selecting the correct concurrency architecture has a significant impact on the design and performance of your applications. This book explains how to leverage the different characteristics of parallel architecture to make your code faster and more efficient.

To start with, you'll understand the basic concurrency concepts and explore patterns around explicit locking, lock free programming, futures & actors. Then, you'll get insights into different concurrency models and parallel algorithms and put them to practice in different scenarios to realize your application's true potential. We'll take you through multithreading design patterns, such as master, slave, leader, follower, map-reduce, and monitor, also helping you to learn hands-on coding using these patterns.

Once you've grasped all of this, you'll move on to solving problems using synchronizer patterns. You'll discover the rationale for these patterns in distributed & parallel applications, followed by studying how future composition, immutability and the monadic flow help create more robust code.

Toward the end of the book, you'll learn about the actor paradigm and actor patterns - the message passing concurrency paradigm.

What you will learn

  • Explore parallel architecture
  • Get acquainted with concurrency models
  • Internalize design themes by implementing multithreading patterns
  • Get insights into concurrent design patterns
  • Discover design principles behind many java threading abstractions
  • Work with functional concurrency patterns

Who this book is for

This is a must-have guide for developers who want to learn patterns to build scalable and high-performing apps. It's assumed that you already have a decent level of programming knowledge.

Trusted by 375,005 students

Access to over 1 million titles for a fair monthly price.

Study more efficiently using our study tools.

Information

Thread Pools

In this chapter, we will be looking at more patterns for staying away from explicit locking and state management. The theme is to let us focus on the business logic and the rest of the boilerplate of explicit thread creation and management handled by a framework.
This set of design patterns yields robust code as we reuse tried and tested, well-proven library code. We start with thread pooling as a major step toward focusing on our business logic as tasks. The pooling patterns give us a facility to run these tasks concurrently.
Firstly, we will cover the need for thread pools and the notion of a task. A task is a manifestation of the command design pattern, decoupling the task definition from its execution. We then look at ExecutorService, the pooling facility given by Java's threading library. Blocking queues are at the heart of this implementation. We will use the blocking queues to home grow our own pooling implementation.
Fork-Join is a major pooling implementation that appeared in Java 7. This is a dynamic thread pool that takes into account the number of cores and the task load. This is also the default dispatcher for the actor systems we will cover in subsequent chapters.
We will look at this API and see the important concept of work stealing.
Finally, we look at the active object design pattern in depth. We will wrap up with a discussion and code implementation of this design pattern.
So, we will be discussing the following topics:
  • More patterns for staying away from explicit locking and state management
  • Thread pooling— need and notion
  • Executor service
  • Fork-join
  • Active object design pattern
For complete code files you can visit https://github.com/PacktPublishing/Concurrent-Patterns-and-Best-Practices

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.
What is the alternative? Instead of spawning a new thread, we can use a thread pool.
Multi-threaded servers typically use thread pools. Each client connection hitting at the server is wrapped as a task and serviced via a thread pool. Java 5 comes with built-in thread pools in the java.util.concurrent package.
The following code shows such a pool in action. A client thread delegates a task to the service, which executes it in the background:
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);

executorService.execute(new Runnable() {
public void run() {
System.out.println("Hey pool!");
}
});

executorService.shutdown();
}
The ExecutorService is an interface. It sends the task to the pool; an ideal pool thread picks up and executes the task. This diagram shows what happens behind the scenes:
How is the task shared, though? There must be a thread-safe channel via which the main thread sends over the task to the pool thread.
The channel is a blocking queue; tasks are enqueued by the client(s) and are dequeued by the threads in the pool. An ideal thread picks up the inserted task and executes it. Other idle threads in the pool will wait for new tasks to come.
This diagram shows this in action:
The pool is shown to have four threads. Each thread picks up the tasks and gets busy executing them. The last task waits in the queue for a thread to free up!

The command design pattern

W...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributors
  5. Preface
  6. Concurrency – An Introduction
  7. A Taste of Some Concurrency Patterns
  8. More Threading Patterns
  9. Thread Pools
  10. Increasing the Concurrency
  11. Functional Concurrency Patterns
  12. Actors Patterns
  13. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Concurrent Patterns and Best Practices by Atul S. Khot in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Languages. We have over one million books available in our catalogue for you to explore.