Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Mastering Concurrency Programming with Java 9, Second Edition
Mastering Concurrency Programming with Java 9, Second Edition

Mastering Concurrency Programming with Java 9, Second Edition: Fast, reactive and parallel application development , Second Edition

Arrow left icon
Profile Icon Javier Fernández González
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8 (4 Ratings)
Paperback Jul 2017 516 pages 2nd Edition
eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Javier Fernández González
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8 (4 Ratings)
Paperback Jul 2017 516 pages 2nd Edition
eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Mastering Concurrency Programming with Java 9, Second Edition

Working with Basic Elements - Threads and Runnables

Execution threads are the core of concurrent applications. When you implement a concurrent application, no matter the language, you have to create different execution threads that run in parallel in a non-deterministic order unless you use a synchronization element (such as a semaphore). In Java you can create execution threads in two ways:

  • Extending the Thread class
  • Implementing the Runnable interface

In this chapter, you will learn how to use these elements to implement concurrent applications in Java. We will cover the following topics:

  • Threads in Java: characteristics and states
  • The Thread class and the Runnable interface
  • First example: matrix multiplication
  • Second example: file search

Threads in Java

Nowadays, computer users (and mobile and tablet users too) use different applications at the same time when they work with their computers. They can be writing a document with a word processor while they're reading the news or posting in a social network and listening to music. They can do all these things at the same time because modern operating systems support multiprocessing.

They can execute different tasks at the same time. But inside an application, you can also do different things at the same time. For example, if you're working with your word processor, you can save the file while you're adding text with bold style. You can do this because the modern programming languages used to write those applications allow programmers to create multiple execution threads inside an application. Each execution thread executes a different task so you can...

First example: matrix multiplication

Matrix multiplication is one of the basic operations that you can do with matrices and a classic problem used in concurrent and parallel programming courses. If you have a matrix A with m rows and n columns and another matrix B with n columns and p columns, you can multiply both matrices and obtain a matrix C with m rows and p columns. You can check https://en.wikipedia.org/wiki/Matrix_multiplication to find a detailed description about this operation.

In this section, we will implement a serial version of an algorithm to multiply two matrices and three different concurrent versions. Then, we will compare the four solutions to see when concurrency gives us a better performance.

Common classes

...

Second example - file search

All operating systems include the option to search for files that verify some conditions in your file system (for example, the name or part of the name, the date of modification, and so on). In our case, we're going to implement an algorithm that looks for a file with a predetermined name. Our algorithms will take the initial path to start the search and the file we're going to look for as input. The JDK provides the ability to walk a directory tree structure, so there should be no need to implement your own in the real world.

Common classes

Both versions of the algorithm will share a common class to store the results of our search. We will call this class Result and it will have two...

Summary

In this chapter, we have presented the most basic elements to create execution threads in Java: the Runnable interface and the Thread class. We can create threads in Java in two different ways:

  • Extending the Thread class and overriding the run() method
  • Implementing the Runnable interface and passing an object of that class to the constructor of the Thread class

The second mechanism is preferred over the first one because they give us more flexibility.

We also learned how the Thread class has different methods that allow us to get information about the thread, change its priority, or wait for its finalization. We have used all these methods in two examples, one to multiply matrices and the other to search files in a directory. In both cases, concurrency gives us better performance but we also have learned that we have to be careful when implementing a concurrent version...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Implement concurrent applications using the Java 9 Concurrency API and its new components
  • Improve the performance of your applications and process more data at the same time, taking advantage of all of your resources
  • Construct real-world examples related to machine learning, data mining, natural language processing, and more

Description

Concurrency programming allows several large tasks to be divided into smaller sub-tasks, which are further processed as individual tasks that run in parallel. Java 9 includes a comprehensive API with lots of ready-to-use components for easily implementing powerful concurrency applications, but with high flexibility so you can adapt these components to your needs. The book starts with a full description of the design principles of concurrent applications and explains how to parallelize a sequential algorithm. You will then be introduced to Threads and Runnables, which are an integral part of Java 9's concurrency API. You will see how to use all the components of the Java concurrency API, from the basics to the most advanced techniques, and will implement them in powerful real-world concurrency applications. The book ends with a detailed description of the tools and techniques you can use to test a concurrent Java application, along with a brief insight into other concurrency mechanisms in JVM.

Who is this book for?

This book is for competent Java developers who have basic understanding of concurrency, but knowledge of effective implementation of concurrent programs or usage of streams for making processes more efficient is not required

What you will learn

  • Master the principles that every concurrent application must follow
  • See how to parallelize a sequential algorithm to obtain better performance without data inconsistencies and deadlocks
  • Get the most from the Java Concurrency API components
  • Separate the thread management from the rest of the application with the Executor component
  • Execute phased-based tasks in an efficient way with the Phaser components
  • Solve problems using a parallelized version of the divide and conquer paradigm with the Fork / Join framework
  • Find out how to use parallel Streams and Reactive Streams
  • Implement the “map and reduce” and “map and collect” programming models
  • Control the concurrent data structures and synchronization mechanisms provided by the Java Concurrency API
  • Implement efficient solutions for some actual problems such as data mining, machine learning, and more

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 17, 2017
Length: 516 pages
Edition : 2nd
Language : English
ISBN-13 : 9781785887949
Vendor :
Oracle
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jul 17, 2017
Length: 516 pages
Edition : 2nd
Language : English
ISBN-13 : 9781785887949
Vendor :
Oracle
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 116.97
Mastering Concurrency Programming with Java 9, Second Edition
€41.99
Java 9 Concurrency Cookbook, Second Edition
€41.99
Java 9 Data Structures and Algorithms
€32.99
Total 116.97 Stars icon
Banner background image

Table of Contents

13 Chapters
The First Step - Concurrency Design Principles Chevron down icon Chevron up icon
Working with Basic Elements - Threads and Runnables Chevron down icon Chevron up icon
Managing Lots of Threads - Executors Chevron down icon Chevron up icon
Getting the Most from Executors Chevron down icon Chevron up icon
Getting Data from Tasks - The Callable and Future Interfaces Chevron down icon Chevron up icon
Running Tasks Divided into Phases - The Phaser Class Chevron down icon Chevron up icon
Optimizing Divide and Conquer Solutions - The Fork/Join Framework Chevron down icon Chevron up icon
Processing Massive Datasets with Parallel Streams - The Map and Reduce Model Chevron down icon Chevron up icon
Processing Massive Datasets with Parallel Streams - The Map and Collect Model Chevron down icon Chevron up icon
Asynchronous Stream Processing - Reactive Streams Chevron down icon Chevron up icon
Diving into Concurrent Data Structures and Synchronization Utilities Chevron down icon Chevron up icon
Testing and Monitoring Concurrent Applications Chevron down icon Chevron up icon
Concurrency in JVM - Clojure and Groovy with the Gpars Library and Scala Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(4 Ratings)
5 star 25%
4 star 25%
3 star 50%
2 star 0%
1 star 0%
Devendra S. Apr 18, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good quality
Amazon Verified review Amazon
Biswajit May 05, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Good book but expecting some more areas to cover, Streams concepts are covered very nicely
Amazon Verified review Amazon
Commuter Jun 03, 2022
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
I have no complaints about the quality or scope of the work, but I feel it could do with more focussed examples that illustrate different aspects of the Java API better. Every topic is only briefly introduced and then followed by a 10-20 page example where you have to spend time and energy to understand the coding problem rather than the specific concurrency tool being discussed. I think it would have been better to have more half-page snippets showcasing key behaviours instead.Brian Goetz's Java Concurrency in Practice provides more focussed examples and is much better at discussing various trade offs between conccurrency options, alas it needs updating to Java 9.
Amazon Verified review Amazon
Richard Oct 28, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Hard to follow due to incomplete code example. Some of the code examples were unable to open due to wrong format.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.