Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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
€8.99 €32.99
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8 (4 Ratings)
eBook 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
€8.99 €32.99
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8 (4 Ratings)
eBook 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 eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781785887451
Vendor :
Oracle
Category :
Languages :
Concepts :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

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

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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.