Mastering Concurrency Programming with Java 8 Table of Contents Mastering Concurrency Programming with Java 8 Credits About the Author About the Reviewers www.PacktPub.com eBooks, discount offers, and more Why subscribe? Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Errata Piracy eBooks, discount offers, and more Questions The First Step – Concurrency Design Principles Basic concurrency concepts Concurrency versus parallelism Synchronization Immutable object Atomic operations and variables Shared memory versus message passing Possible problems in concurrent applications Data race Deadlock Livelock Resource starvation Priority inversion A methodology to design concurrent algorithms The starting point – a sequential version of the algorithm Step 1 – analysis Step 2 – design Step 3 – implementation Step 4 – testing Step 5 – tuning Conclusion Java concurrency API Basic concurrency classes Synchronization mechanisms Executors The Fork/Join framework Parallel streams Concurrent data structures Concurrency design patterns Signaling Rendezvous Mutex Multiplex Barrier Double-checked locking Read-write lock Thread pool Thread local storage The Java memory model Tips and tricks to design concurrent algorithms Identify the correct independent tasks Implement concurrency at the highest possible level Take scalability into account Use thread-safe APIs Never assume an execution order Prefer local thread variables over static and shared when possible Find the more easily parallelizable version of the algorithm Using immutable objects when possible Avoiding deadlocks by ordering the locks Using atomic variables instead of synchronization Holding locks for as short a time as possible Taking precautions using lazy initialization Avoiding the use of blocking operations inside a critical section Summary Managing Lots of Threads – Executors An introduction to executors Basic characteristics of executors Basic components of the executor framework First example – the k-nearest neighbors algorithm K-nearest neighbors – serial version K-nearest neighbors – a fine-grained concurrent version K-nearest neighbors – a coarse-grained concurrent version Comparing the solutions The second example – concurrency in a client/server environment Client/server – serial version The DAO part The command part The server part Client/server – parallel version The server part The command part Extra components of the concurrent server The status command The cache system The log system Comparing the two solutions Other methods of interest Summary Getting the Maximum from Executors Advanced characteristics of executors Cancellation of tasks Scheduling the execution of tasks Overriding the executor methods Changing some initialization parameters The first example – an advanced server application The ServerExecutor class The statistics object The rejected task controller The executor tasks The executor The command classes The ConcurrentCommand class Concrete commands The server part The ConcurrentServer class The RequestTask class The client part The second example – executing periodic tasks The common parts The basic reader The advanced reader Additional information about executors Summary Getting Data from the Tasks – The Callable and Future Interfaces Introducing the Callable and Future interfaces The Callable interface The Future interface First example – a best-matching algorithm for words The common classes A best-matching algorithm – the serial version The BestMatchingSerialCalculation class The BestMachingSerialMain class A best-matching algorithm – the first concurrent version The BestMatchingBasicTask class The BestMatchingBasicConcurrentCalculation class A best-matching algorithm – the second concurrent version The word exists algorithm – a serial version The ExistSerialCalculation class The ExistSerialMain class The word exists algorithm – the concurrent version The ExistBasicTasks class The ExistBasicConcurrentCalculation class The ExistBasicConcurrentMain class Comparing the solutions Best-matching algorithms Exist algorithms The second example – creating an inverted index for a collection of documents Common classes The Document class The DocumentParser class The serial version The first concurrent version – a task per document The IndexingTask class The InvertedIndexTask class The ConcurrentIndexing class The second concurrent version – multiple documents per task The MultipleIndexingTask class The MultipleInvertedIndexTask class The MultipleConcurrentIndexing class Comparing the solutions Other methods of interest Summary Running Tasks Divided into Phases – The Phaser Class An introduction to the Phaser class Registration and deregistration of participants Synchronizing phase changes Other functionalities First example – a keyword extraction algorithm Common classes The Word class The Keyword class The Document class The DocumentParser class The serial version The concurrent version The KeywordExtractionTask class The ConcurrentKeywordExtraction class Comparing the two solutions The second example – a genetic algorithm Common classes The Individual class The GeneticOperators class The serial version The SerialGeneticAlgorithm class The SerialMain class The concurrent version The SharedData class The GeneticPhaser class The ConcurrentGeneticTask class The ConcurrentGeneticAlgorithm class The ConcurrentMain class Comparing the two solutions The Lau15 dataset The Kn57 dataset Conclusions Summary Optimizing Divide and Conquer Solutions – The Fork/Join Framework An introduction to the Fork/Join framework Basic characteristics of the Fork/Join framework Limitations of the Fork/Join framework Components of the Fork/Join framework The first example – the k-means clustering algorithm The common classes The VocabularyLoader class The Word, Document, and DocumentLoader classes The DistanceMeasurer class The DocumentCluster class The serial version The SerialKMeans class The concurrent version Two tasks for the Fork/Join framework – AssignmentTask and UpdateTask The ConcurrentKMeans class The ConcurrentMain class Comparing the solutions The second example – a data filtering algorithm Common parts M machine-learning algorithm about / An introduction to executors mechanisms, for avoiding deadlocks ignore / Deadlock detection / Deadlock prevention / Deadlock avoidance / Deadlock Mercurial references / Installing Java Pathfinder merge sort algorithm about / The third example – the merge sort algorithm shared classes / Shared classes serial version / The serial version concurrent version / The concurrent version two versions, comparing / Comparing the two versions message passing about / Shared memory versus message passing message passing techniques synchronous / Big-block synchronization mechanisms asynchronous / Big-block synchronization mechanisms methodology, design concurrent algorithms about / A methodology to design concurrent algorithms starting point / The starting point – a sequential version of the algorithm analysis / Step 1 – analysis design / Step 2 – design implementation / Step 3 – implementation testing / Step 4 – testing tuning / Step 5 – tuning conclusion / Conclusion methods used, for registering participant / Registration and deregistration of participants used, for synchronizing phase change / Synchronizing phase changes methods, ForkJoinTask class fork() method / Basic characteristics of the Fork/Join framework join() method / Basic characteristics of the Fork/Join framework monitor about / Synchronization multiple documents per task, second concurrent version MultipleIndexingTask class / The MultipleIndexingTask class MultipleInvertedIndexTask class / The MultipleInvertedIndexTask class MultipleConcurrentIndexing class / The MultipleConcurrentIndexing class MultithreadedTC concurrent applications, testing with / Testing concurrent applications with MultithreadedTC mutual exclusion about / Synchronization N non-blocking data structure about / The log system non-blocking operations methods, implementing / Other methods of interest numerical summarization application about / The first example – a numerical summarization application concurrent version / The concurrent version serial version / The serial version two versions, comparing / Comparing the two versions numeric summarization algorithm about / A numeric summarization algorithm O operation used, for serial streams / Comparing the two versions optimization and search problems about / The second example – a genetic algorithm defining / A genetic algorithm P parallel streams about / Parallel streams Stream interface / Parallel streams Optional interface / Parallel streams Collectors / Parallel streams Lambda expressions / Parallel streams parallel version, client/server model defining / Client/server – parallel version server part / The server part command part / The command part periodic tasks executing / The second example – executing periodic tasks common parts / The common parts basic reader / The basic reader advanced reader / The advanced reader Phaser class about / An introduction to the Phaser class characteristics / An introduction to the Phaser class participants, registering / Registration and deregistration of participants participants, deregistration / Registration and deregistration of participants phase change, synchronizing / Synchronizing phase changes other functionalities / Other functionalities methods, defining / Other functionalities phases defining, for keywords / First example – a keyword extraction algorithm phases, generation Selection / The second example – a genetic algorithm Crossover / The second example – a genetic algorithm Mutation / The second example – a genetic algorithm phases, recommendation system first phase / A recommendation system using the Map and Collect model second phase / A recommendation system using the Map and Collect model third phase / A recommendation system using the Map and Collect model phenotypes about / The second example – a genetic algorithm portability about / Conclusion problems, in concurrent applications about / Possible problems in concurrent applications data race / Data race deadlock / Deadlock livelock / Livelock resource starvation / Resource starvation priority inversion / Priority inversion Producer/Consumer design pattern about / The second example – executing periodic tasks Q queue about / BlockingQueue R recommendation system defining / The second example – a recommendation system about / The second example – a recommendation system, A recommendation system using the Map and Collect model common classes / Common classes main class / The recommendation system – the main class ConcurrentLoaderAccumulator class / The ConcurrentLoaderAccumulator class serial version / The serial version two versions, comparing / Comparing the two versions defining, Map and Collect model used / A recommendation system using the Map and Collect model reduced document query about / The second approach – reduced document query limitedMapper() method / The limitedMapper() method RSS feed reader about / The second example – executing periodic tasks S SAX (Simple API for XML) about / The common parts scalability about / Conclusion scheduled executor about / The advanced reader search algorithm, without indexing defining / A search algorithm without indexing second concurrent version, best-matching algorithm about / A best-matching algorithm – the second concurrent version semaphore about / Synchronization SerialMergeSort class references / The SerialMergeSort class serial version, best-matching algorithm about / A best-matching algorithm – the serial version BestMatchingSerialCalculation class / The BestMatchingSerialCalculation class BestMachingSerialMain class / The BestMachingSerialMain class serial version, client/server model defining / Client/server – serial version DAO part / The DAO part command part / The command part server part / The server part serial version, data filtering algorithm about / The serial version SerialSearch class / The SerialSearch class SerialMain class / The SerialMain class serial version, genetic algorithm about / The serial version SerialGeneticAlgorithm class / The SerialGeneticAlgorithm class SerialMain class / The SerialMain class serial version, k-means clustering algorithm about / The serial version SerialKMeans class / The SerialKMeans class SerialMain class / The SerialKMeans class serial version, merge sort algorithm about / The serial version SerialMergeSort class / The SerialMergeSort class SerialMetaData class / The SerialMetaData class serial version, social network about / The serial version two versions, comparing / Comparing the two versions serial version, word exists algorithm defining / The word exists algorithm – a serial version ExistSerialCalculation class / The ExistSerialCalculation class ExistSerialMain class / The ExistSerialMain class server application defining / The first example – an advanced server application new characteristics, implementing / The first example – an advanced server application ServerExecutor class / The ServerExecutor class command classes / The command classes server part / The server part client part / The client part ServerExecutor class about / The ServerExecutor class statistics object / The statistics object rejected task controller / The rejected task controller executor tasks / The executor tasks executor / The executor server part about / The server part ConcurrentServer class / The ConcurrentServer class RequestTask classification / The RequestTask class shared memory about / Shared memory versus message passing, The main class of the document clustering application short-circuit operation about / The word exists algorithm – the concurrent version simplicity about / Conclusion Singleton pattern about / Concurrency design patterns SOA (Service-Oriented Architecture) about / The second example – concurrency in a client/server environment social network common contacts / The third example – common contacts in a social network references / The third example – common contacts in a social network base classes / Base classes concurrent version / The concurrent version serial version / The serial version speedup about / Step 5 – tuning states, phaser Active / Other functionalities Termination / Other functionalities stream elements / Using streams to collect data, The ConcurrentStringAccumulator class streams about / An introduction to streams characteristics / Basic characteristics of streams sections, defining / Sections of a stream sources / Sources of a stream intermediate operations / Intermediate operations terminal operations / Terminal operations MapReduce, versus MapCollect / MapReduce versus MapCollect used, for collecting data / Using streams to collect data synchronization about / Synchronization control synchronization / Synchronization data access synchronization / Synchronization synchronization mechanisms synchronized keyword / Synchronization mechanisms Lock interface / Synchronization mechanisms Semaphore class / Synchronization mechanisms CountDownLatch class / Synchronization mechanisms CyclicBarrier class / Synchronization mechanisms Phaser class / Synchronization mechanisms defining / Big-block synchronization mechanisms shared memory / Big-block synchronization mechanisms message passing / Big-block synchronization mechanisms systems, k-means clustering defining / The four systems of k-means clustering Reader system / The Reader system Indexer system / The Indexer system Mapper system / The Mapper system Clustering system / The Clustering system T task decomposition about / Step 2 – design task per document, first concurrent version IndexingTask class / The IndexingTask class InvertedIndexTask class / The InvertedIndexTask class ConcurrentIndexing class / The ConcurrentIndexing class tasks executing / Introducing the Callable and Future interfaces term frequency about / The second example – creating an inverted index for a collection of documents term frequency-inverse document frequency (tf-idf) about / The first example – the k-means clustering algorithm TF (term frequency) about / First example – a keyword extraction algorithm Tf-Idf (term frequency-inverse document frequency) about / An example of a document clustering application TF-IDF measure about / First example – a keyword extraction algorithm thread-safe about / Synchronization thread safety about / Synchronization tips and tricks, design concurrent algorithms correct independent tasks, identifying / Identify the correct independent tasks concurrency, implementing at higher possible level / Implement concurrency at the highest possible level scalability, taking into account / Take scalability into account thread-safe APIs, using / Use thread-safe APIs execution of tasks / Never assume an execution order local thread variables, preferring over static / Prefer local thread variables over static and shared when possible easier parallelizable version of algorithm, finding / Find the more easily parallelizable version of the algorithm immutable objects, using / Using immutable objects when possible deadlocks, avoiding by ordering locks / Avoiding deadlocks by ordering the locks atomic variables, using instead of synchronization / Using atomic variables instead of synchronization locks, holding / Holding locks for as short a time as possible precautions, taking with lazy initialization / Taking precautions using lazy initialization blocking operations, avoiding in critical section / Avoiding the use of blocking operations inside a critical section Travel Salesman Problem (TSP) about / The second example – a genetic algorithm characteristics / The second example – a genetic algorithm U UCI Machine Learning Repository about / First example – the k-nearest neighbors algorithm, The first example – a numerical summarization application UK Advanced Cryptics Dictionary (UKACD) about / First example – a best-matching algorithm for words W word exists algorithm and best-matching algorithms, comparing / Exist algorithms word list URL / First example – a best-matching algorithm for words work-stealing algorithm about / An introduction to the Fork/Join framework worker-threads about / Basic characteristics of executors World Bank URL / The second example – concurrency in a client/server environment World Development Indicators about / The second example – concurrency in a client/server environment ... Mastering Concurrency Programming with Java 8 Table of Contents Mastering Concurrency Programming with Java 8 Credits About the Author About the Reviewers... Installing Java Pathfinder Running Java Pathfinder Summary Index Mastering Concurrency Programming with Java 8 Mastering Concurrency Programming with Java 8 Copyright © 2016 Packt Publishing All rights reserved... Step 5 – tuning Conclusion Java concurrency API Basic concurrency classes Synchronization mechanisms Executors The Fork/Join framework Parallel streams Concurrent data structures Concurrency design patterns