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)

Concurrent hashing


 A hash table is typically implemented as an array. Each array entry is a list of one or more items. A hash function maps values to indices in this array. Each Java object has a hashCode() method that gives an integer for an object. This number's modulo to the array length gives us an index into this array.

For any operation to add, remove, or check whether the set contains an item, we first index into the array (or table), and then perform the rest of the processing.

The basic idea is that, given a table and a hash function, we provide the contains(key),add(key, value), and remove(key) methods, with a constant average time.  

The following diagram shows how a typical hash table works:

The key is run through a hash function (f()).The resulting modulo of the hash function to the number of entries gives us a hash—that is, an offset into the table. 

We use the hash table to represent a hash set. A set contains unique elements.

The following list shows the method contracts:

  • The add...