-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Mastering Concurrency Programming with Java 9, Second Edition
By :

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.
The basic classes of the Concurrency API are:
Factory
design pattern, that you can use to create customized threads
The Java Concurrency API includes different synchronization mechanisms that allow you to:
The following mechanisms are the most important synchronization mechanisms:
synchronized
keyword allows you to define a critical section in a block of code or in an entire method.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 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:
execute()
method common to all executorsRunnable
interface - a separate task that can return a valueCallable
interface and to control its statusThe 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 tasksForkJoinTask
: This is a task that can be executed in the ForkJoinPool
classForkJoinWorkerThread
: This is a thread that is going to execute tasks in the ForkJoinPool
classStreams 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:
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:
These are some of the data structures:
ConcurrentLinkedDeque
: This is a non-blocking listConcurrentLinkedQueue
: This is a non-blocking queueLinkedBlockingDeque
: This is a blocking listLinkedBlockingQueue
: This is a blocking queuePriorityBlockingQueue
: This is a blocking queue that orders its elements based on their priorityConcurrentSkipListMap
: This is a non-blocking navigable mapConcurrentHashMap
: This is a non-blocking hash mapAtomicBoolean
, AtomicInteger
, AtomicLong
, and AtomicReference
: These are atomic implementations of the basic Java data types