Mastering Concurrency Programming with Java 8
eBook - ePub

Mastering Concurrency Programming with Java 8

Javier Fernandez Gonzalez

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

Mastering Concurrency Programming with Java 8

Javier Fernandez Gonzalez

Book details
Book preview
Table of contents
Citations

About This Book

Master the principles and techniques of multithreaded programming with the Java 8 Concurrency API

About This Book

  • Implement concurrent applications using the Java 8 Concurrency API and its new components
  • Improve the performance of your applications or process more data at the same time, taking advantage of all of your resources.
  • Construct real-world examples related to machine learning, data mining, image processing, and client/server environments

Who This Book Is For

If you are a competent Java developer with a good understanding of concurrency but have no knowledge of how to effectively implement concurrent programs or use streams to make processes more efficient, then this book is for you.

What You Will Learn

  • Design concurrent applications by converting a sequential algorithm into a concurrent one
  • Discover how to avoid all the possible problems you can get in concurrent algorithms
  • Use the Executor framework to manage concurrent tasks without creating threads
  • Extend and modify Executors to adapt their behavior to your needs
  • Solve problems using the divide and conquer technique and the Fork/Join framework
  • Process massive data sets with parallel streams and Map/Reduce implementation
  • Control data-race conditions using concurrent data structures and synchronization mechanisms
  • Test and monitor concurrent applications

In Detail

Concurrency programming allows several large tasks to be divided into smaller sub-tasks, which are further processed as individual tasks that run in parallel. All the sub-tasks are combined together once the required results are achieved; they are then merged to get the final output. The whole process is very complex. This process goes from the design of concurrent algorithms to the testing phase where concurrent applications need extra attention. Java includes a comprehensive API with a lot of ready-to-use components to implement powerful concurrency applications in an easy way, but with a high flexibility to adapt these components to your needs.

The book starts with a full description of design principles of concurrent applications and how to parallelize a sequential algorithm. We'll show you how to use all the components of the Java Concurrency API from basics to the most advanced techniques to implement them in powerful concurrency applications in Java.

You will be using real-world examples of complex algorithms related to machine learning, data mining, natural language processing, image processing in client / server environments. Next, you will learn how to use the most important components of the Java 8 Concurrency API: the Executor framework to execute multiple tasks in your applications, the phaser class to implement concurrent tasks divided into phases, and the Fork/Join framework to implement concurrent tasks that can be split into smaller problems (using the divide and conquer technique). Toward the end, we will cover the new inclusions in Java 8 API, the Map and Reduce model, and the Map and Collect model. The book will also teach you about the data structures and synchronization utilities to avoid data-race conditions and other critical problems. Finally, the book ends with a detailed description of the tools and techniques that you can use to test a Java concurrent application.

Style and approach

A complete guide implementing real-world examples with algorithms related to machine learning, data mining, and natural language processing in client/server environments. All the examples are explained in a step-by-step approach.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
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 here.
Is Mastering Concurrency Programming with Java 8 an online PDF/ePUB?
Yes, you can access Mastering Concurrency Programming with Java 8 by Javier Fernandez Gonzalez in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.

Information

Year
2016
ISBN
9781785886126
Edition
1

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
1. 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
2. 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
3. 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
4. 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
5. 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
6. 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...

Table of contents