Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Mastering Concurrency Programming with Java 8
  • Table Of Contents Toc
  • Feedback & Rating feedback
Mastering Concurrency Programming with Java 8

Mastering Concurrency Programming with Java 8

By : Javier Fernández González
3 (4)
close
close
Mastering Concurrency Programming with Java 8

Mastering Concurrency Programming with Java 8

3 (4)
By: Javier Fernández González

Overview of this book

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.
Table of Contents (13 chapters)
close
close
12
Index

Java Concurrency API

The Java programming language has a very rich concurrency API. It contains classes to manage the basic elements of concurrency, such as Thread, Lock, and Semaphore, and classes that implement very high-level synchronization mechanisms, such as the executor framework or the new parallel Stream API.

In this section, we will cover the basic classes that form the concurrency API.

Basic concurrency classes

The basic classes of the Concurrency API are:

  • The Thread class: This class represents all the threads that execute a concurrent Java application
  • The Runnable interface: This is another way to create concurrent applications in Java
  • The ThreadLocal class: This is a class to store variables locally to a thread
  • The ThreadFactory interface: This is the base of the Factory design pattern, that you can use to create customized threads

Synchronization mechanisms

The Java Concurrency API includes different synchronization mechanisms that allow you to:

  • Define a critical section to access a shared resource
  • Synchronize different tasks at a common point

The following mechanisms are the most important synchronization mechanisms:

  • The synchronized keyword: The synchronized keyword allows you to define a critical section in a block of code or in an entire method.
  • The Lock interface: Lock provides a more flexible synchronization operation than the synchronized keyword. There are different kinds of Locks: ReentrantLock, to implement a Lock that can be associated with a condition; ReentrantReadWriteLock that separates the read and write operations; and StampedLock, a new feature of Java 8 that includes three modes for controlling read/write access.
  • The Semaphore class: The class that implements the classical semaphore to implement the synchronization. Java supports binary and general semaphores.
  • The CountDownLatch class: A class that allows a task to wait for the finalization of multiple operations.
  • The CyclicBarrier class: A class that allows the synchronization of multiple threads at a common point.
  • The Phaser class: A class that allows you to control the execution of tasks divided into phases. None of the tasks advance to the next phase until all of the tasks have finished the current phase.

Executors

The executor framework is a mechanism that allows you to separate thread creation and management for the implementation of concurrent tasks. You don't have to worry about the creation and management of threads, only to create tasks and send them to the executor. The main classes involved in this framework are:

  • The Executor and ExecutorService interface: This includes the execute() method common to all executors
  • ThreadPoolExecutor: This is a class that allows you to get an executor with a pool of threads and, optionally, define a maximum number of parallel tasks
  • ScheduledThreadPoolExecutor: This is a special kind of executor to allow you to execute tasks after a delay or periodically
  • Executors: This is a class that facilitates the creation of executors
  • The Callable interface: This is an alternative to the Runnable interface - a separate task that can return a value
  • The Future interface: This is an interface that includes the methods to obtain the value returned by a Callable interface and to control its status

The fork/join framework

The fork/join framework defines a special kind of executor specialized in the resolution of problems with the divide and conquer technique. It includes a mechanism to optimize the execution of the concurrent tasks that solve these kinds of problems. Fork/Join is specially tailored for fine-grained parallelism, as it has very low overhead in order to place the new tasks into the queue and take queued tasks for execution. The main classes and interfaces involved in this framework are:

  • ForkJoinPool: This is a class that implements the executor that is going to run the tasks
  • ForkJoinTask: This is a task that can be executed in the ForkJoinPool class
  • ForkJoinWorkerThread: This is a thread that is going to execute tasks in the ForkJoinPool class

Parallel streams

Streams and lambda expressions were the two most important new features of the Java 8 version. Streams have been added as a method in the Collection interface and other data sources and allow the processing of all elements of a data structure generating new structures, filtering data, and implementing algorithms using the map and reduce technique.

A special kind of stream is a parallel stream that realizes its operations in a parallel way. The most important elements involved in the use of parallel streams are:

  • The Stream interface: This is an interface that defines all the operations that you can perform on a stream.
  • Optional: This is a container object that may or may not contain a non-null value.
  • Collectors: This is a class that implements reduction operations that can be used as part of a stream sequence of operations.
  • Lambda expressions: Streams have been thought of to work with Lambda expressions. Most of stream methods accept a lambda expression as a parameter. This allows you to implement a more compact version of operations.

Concurrent data structures

Normal data structures of the Java API (ArrayList, Hashtable, and so on) are not ready to work in a concurrent application unless you use an external synchronization mechanism. If you use it, you will be adding a lot of extra computing time to your application. If you don't use it, it's probable that you will add race conditions to your application. If you modify them from several threads and race conditions occur, you may experience various exceptions (such as, ConcurrentModificationException and ArrayIndexOutOfBoundsException), silent data loss, or your program may even get stuck in an endless loop.

The Java Concurrency API includes a lot of data structures that can be used in concurrent applications without risk. We can classify them into two groups:

  • Blocking data structures: These include methods that block the calling task when, for example, the data structure is empty and you want to get a value.
  • Non-blocking data structures: If the operation can be made immediately, it won't block the calling tasks. It returns a null value or throws an exception.

These are some of the data structures:

  • ConcurrentLinkedDeque: This is a non-blocking list
  • ConcurrentLinkedQueue: This is a non-blocking queue
  • LinkedBlockingDeque: This is a blocking list
  • LinkedBlockingQueue: This is a blocking queue
  • PriorityBlockingQueue: This is a blocking queue that orders its elements based on their priority
  • ConcurrentSkipListMap: This is a non-blocking navigable map
  • ConcurrentHashMap: This is a non-blocking hash map
  • AtomicBoolean, AtomicInteger, AtomicLong, and AtomicReference: These are atomic implementations of the basic Java data types
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Mastering Concurrency Programming with Java 8
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon