Now, in Java 8, it has included the stream APIand new methods and classes to facilitate the implementation of concurrent applications.This book covers the most important elements of the
Trang 3Mastering Concurrency Programming with Java 8
Trang 4Mastering Concurrency Programming with Java 8Credits
Deadlock
Trang 7The basic reader
The advanced reader
Additional information about executors
Summary
Trang 9The second concurrent version – multiple documents per taskThe MultipleIndexingTask class
Trang 10Comparing the solutions
The second example – a data filtering algorithm
Common parts
Trang 12The ConcurrentFileLoader class
The fifth approach – using our own executor
Getting data from the inverted index – the ConcurrentData classGetting the number of words in a file
Getting the average tfxidf value in a file
Getting the maximum and minimum tfxidf values in the indexThe ConcurrentMain class
Trang 13Comparing the solutions
Summary
8 Processing Massive Datasets with Parallel Streams – The Map and Collect ModelUsing streams to collect data
Trang 14Interfaces
BlockingQueueBlockingDequeConcurrentMapTransferQueueClasses
LinkedBlockingQueueConcurrentLinkedQueueLinkedBlockingDequeConcurrentLinkedDequeArrayBlockingQueueDelayQueue
LinkedTransferQueuePriorityBlockingQueueConcurrentHashMapUsing the new features
First example with ConcurrentHashMap
The forEach() methodThe search() methodThe reduce() methodThe compute() methodAnother example with ConcurrentHashMap
Trang 15The spliterator() methodAtomic variables
10 Integration of Fragments and Implementation of AlternativesBig-block synchronization mechanisms
Implementation of alternatives with concurrent programmingThe k-nearest neighbors’ algorithm
Building an inverted index of a collection of documents
A best-matching algorithm for words
A genetic algorithm
A keyword extraction algorithm
Trang 18Mastering Concurrency Programming with Java 8
Trang 24Javier Fernández González is a software architect with almost 15 years’ experience with
Java technologies He has worked as a teacher, researcher, programmer, analyst, writer,and now as an architect in all types of projects related to Java, especially J2EE As ateacher, he has taught over 1,000 hours of training in basic Java, J2EE, and Struts
framework As a researcher, he has worked in the field of information retrieval,
developing applications in order to process large amounts of data in Java and has been apart of several journal articles and conference presentations as a coauthor In recent years,
he has worked on developing J2EE web applications for various clients from differentsectors (public administration, insurance, healthcare, transportation, and so on) Currently,
he is working as a software architect at Capgemini, which includes developing and
maintaining applications for an insurance company Also, he is the author of the book
Java 7 Concurrency Cookbook, Packt Publishing.
To Nuria, Paula, and Pelayo, for your infinite love and patience
Trang 26Antonio Gomes Rodrigues has worked with high-traffic websites, traders applications,
Cloud applications, and so on He has experience of working with many performancetools, such as JProfiler, Yourkit, VisualVM, Dynatrace, AppDynamics, Introscope,
NewRelic, JMeter, LoadRunner, and so on To put in place, he has worked on performancestrategies, load testing, training, troubleshooting, and so on He shares his knowledge inhis French blog (http://arodrigues.developpez.com/), conferences, and book reviews
I would like to thank my wife Aurélie for her support and my child Timothée
Bradley Symons graduated with a BSc (Hons) degree in Computer Science and has
gained the Oracle Java Professional Programmer certification His current occupation is as
a Java server-side developer, but he has previously worked with a variety of languages,including Ruby and Python With over a decade of coding experience, he has worked for avariety of business sectors, including AI, Finance, Travel, and Entertainment He is highlyinterested in Spring, AOP, and concurrency Currently, he is learning Cuba among otherrecent developments within the Java world He is well-regarded as an avid follower ofcoding best practices, refactoring patterns, and TDD He personally expresses his
admiration for the technical contributions from the legendary Martin Fowler
I would like to thank Jim Combes, my old team lead, for offering me the role as a JavaDeveloper and allowing my enthusiastic interest in Java to grow and develop over theyears
Tagir Valeev, PhD, is a research assistant in A.P Ershov Institute of Informatics Systems,
Novosibirsk, Russia, and a lead developer in Development Group Ltd., Novosibirsk,
Russia He is a Java programming professional, a contributor to the FindBugs project(Java static analysis tool), and the author of the StreamEx project, which enhances theJava 8 Stream API He is also officially an OpenJDK author, contributing enhancementsand bug fixes in Stream API implementation He answered many questions on
StackOverflow related to Stream API and Java concurrency
Big thanks to my wife Ekaterina and my son Artyom, for support and patience whichhelped me so much to finish the reviewing
Trang 28www.PacktPub.com
Trang 29Did you know that Packt offers eBook versions of every book published, with PDF andePub files available? You can upgrade to the eBook version at www.PacktPub.com and as
a print book customer, you are entitled to a discount on the eBook copy Get in touch with
us at < customercare@packtpub.com > for more details
At www.PacktPub.com, you can also read a collection of free technical articles, sign upfor a range of free newsletters and receive exclusive discounts and offers on Packt booksand eBooks
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt’s online digitalbook library Here, you can search, access, and read Packt’s entire library of books
Trang 30Fully searchable across every book published by PacktCopy and paste, print, and bookmark content
On demand and accessible via a web browser
Trang 32Nowadays, computer systems (and other related systems, such as tablets or smartphones)allow you to do several tasks simultaneously This can be possible because they haveconcurrent operating systems that control several tasks at the same time You can alsohave one application that executes several tasks (read a file, show a message, or read dataover a network) if you work with the concurrency API of your favorite programminglanguage Java includes a very powerful concurrency API that allows you to implementany kind of concurrency application with little effort This API increases the featuresprovided to programmers in every version Now, in Java 8, it has included the stream APIand new methods and classes to facilitate the implementation of concurrent applications.This book covers the most important elements of the Java concurrency API, showing youhow to use them in real-world applications These elements are as follows:
The executor framework, to control the execution of lots of task
The Phaser class, to execute tasks that can be divided into phases
The Fork/Join framework, to execute the tasks that solve a problem using the divideand conquer technique
The stream API, to process big sources of data
Concurrent data structures, to store the data in concurrent applications
Synchronization mechanisms, to organize concurrent tasks
However, it includes much more: a methodology to design concurrency applications,design patterns, tips and tricks to implement good concurrency applications, and tools andtechniques to test concurrency applications
Trang 33Chapter 1, The First Step – Concurrency Design Principles, will teach you the design
principles of concurrency applications They will also learn the possible problems ofconcurrency applications and a methodology to design them followed by some designpatterns, tips, and tricks
Chapter 2, Managing Lots of Threads – Executors, will teach you the basic principles of
the executor framework This framework allows you to work with lots of threads withoutcreating or managing them You will implement the k-nearest neighbors algorithm and abasic client/server application
Chapter 3, Getting the Maximum from Executors, will teach you some advanced
characteristics of executors, including cancelation and scheduling of tasks to execute atask after a delay or every certain period of time You will implement an advanced
client/server application and a news reader
Chapter 4, Getting Data from the Tasks – The Callable and Future Interfaces, will teach
you how to work in an executor with tasks that return a result using the Callable andFuture interfaces You will implement a best-matching algorithm and an application tobuild an inverted index
Chapter 5, Running Tasks Divided into Phases – The Phaser class, will teach you how to
use the Phaser class to execute tasks that can be divided into phases in a concurrent way.You will implement a keyword extraction algorithm and a genetic algorithm
Chapter 6, Optimizing Divide and Conquer Solutions – The Fork/Join Framework, will
teach you how to use a special kind of executor optimized by those problems that can beresolved using the divide and conquer technique: the Fork/Join framework and its work-stealing algorithm You will implement the k-means clustering algorithm, a data filteringalgorithm, and the merge-sort algorithm
Chapter 7, Processing Massive Datasets with Parallel Streams – The Map and Reduce
Model, will teach you how to work with streams to process big datasets In this chapter,
you will learn how to implement map and reduce applications using the stream API andmuch more functions of streams You will implement a numerical summarization
Chapter 9, Diving into Concurrent Data Structures and Synchronization Utilities, will
teach you how to work with the most important concurrent data structures (data structuresthat can be used in concurrent applications without causing data race conditions) and all
Trang 34Chapter 10, Integration of Fragments and Implementation of Alternatives, will teach you
how to implement a big application made by fragments of concurrent applications withtheir own concurrency techniques using shared memory or message passing You will alsolearn different implementation alternatives to the examples presented in the book
Chapter 11, Testing and Monitoring Concurrent Applications, teaches you how to obtain
information about the status of some of the Java concurrency API elements (thread, lock,executor, and so on) You will also learn how to monitor a concurrent application using theJava VisualVM application and how to test concurrent applications with the
MultithreadedTC library and the Java Pathfinder application
Trang 36To follow this book, you need basic knowledge of the Java programming language Abasic knowledge of concurrency concepts is welcome too
Trang 38If you are a Java developer who knows the basic principles of concurrent programmingbut you want to get an expert knowledge of the Java concurrency API to develop
optimized applications that takes advantage of all the hardware resources of computers,then this book is for you
Trang 42Feedback from our readers is always welcome Let us know what you think about thisbook—what you liked or disliked Reader feedback is important for us as it helps usdevelop titles that you will really get the most out of
To send us general feedback, simply e-mail < feedback@packtpub.com >, and mention thebook’s title in the subject of your message
If there is a topic that you have expertise in and you are interested in either writing orcontributing to a book, see our author guide at www.packtpub.com/authors
Trang 44Now that you are the proud owner of a Packt book, we have a number of things to helpyou to get the most from your purchase
Trang 45Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
Trang 46Although we have taken every care to ensure the accuracy of our content, mistakes dohappen If you find a mistake in one of our books—maybe a mistake in the text or thecode—we would be grateful if you could report this to us By doing so, you can save otherreaders from frustration and help us improve subsequent versions of this book If you findany errata, please report them by visiting http://www.packtpub.com/submit-errata,
selecting your book, clicking on the Errata Submission Form link, and entering the
details of your errata Once your errata are verified, your submission will be accepted andthe errata will be uploaded to our website or added to any list of existing errata under theErrata section of that title
To view the previously submitted errata, go to
https://www.packtpub.com/books/content/support and enter the name of the book in the
search field The required information will appear under the Errata section.
Trang 47Piracy of copyrighted material on the Internet is an ongoing problem across all media AtPackt, we take the protection of our copyright and licenses very seriously If you comeacross any illegal copies of our works in any form on the Internet, please provide us withthe location address or website name immediately so that we can pursue a remedy
Please contact us at < copyright@packtpub.com > with a link to the suspected piratedmaterial
We appreciate your help in protecting our authors and our ability to bring you valuablecontent
Trang 48Did you know that Packt offers eBook versions of every book published, with PDF andePub files available? You can upgrade to the eBook version at www.PacktPub.com and as
a print book customer, you are entitled to a discount on the eBook copy Get in touch with
us at < customercare@packtpub.com > for more details
At www.PacktPub.com, you can also read a collection of free technical articles, sign upfor a range of free newsletters, and receive exclusive discounts and offers on Packt booksand eBooks
Trang 49If you have a problem with any aspect of this book, you can contact us at
< questions@packtpub.com >, and we will do our best to address the problem.
Trang 51Chapter 1 The First Step – Concurrency Design Principles
Users of computer systems are always looking for better performance for their systems.They want to get higher quality videos, better video games, and faster network speed.Some years ago, processors gave better performance to users by increasing their speed.But now, processors don’t increase their speed Instead of this, they add more cores so thatthe operating system can execute more than one task at a time This is named
concurrency Concurrent programming includes all the tools and techniques to have
multiple tasks or processes running at the same time in a computer, communicating andsynchronizing between them without data loss or inconsistency In this chapter, we willcover the following topics:
Trang 52First of all, let’s present the basic concepts of concurrency You must understand theseconcepts to follow the rest of the book
Trang 53Concurrency and parallelism are very similar concepts Different authors give differentdefinitions to these concepts The most accepted definition talks about concurrency whenyou have more than one task in a single processor with a single core and the operatingsystem’s task scheduler quickly switches from one task to another, so it seems that all thetasks run simultaneously The same definition talks about parallelism when you have morethan one task that run simultaneously at the same time, in a different computer, processor,
or core inside a processor
Another definition talks about concurrency when you have more than one task (differenttasks) running simultaneously on your system One more definition discusses parallelismwhen you have different instances of the same task running simultaneously over differentparts of a dataset
The last definition that we include talks about parallelism when you have more than onetask that runs simultaneously in your system and talks about concurrency to explain thedifferent techniques and mechanisms programmers have to synchronize with the tasks andtheir access to shared resources
As you can see, both concepts are very similar and this similarity has increased with thedevelopment of multicore processors