O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java 9 Concurrency Cookbook - Second Edition

Book Description

Master the art of fast, effective Java development with the power of concurrent and parallel programming

About This Book

  • Get detailed coverage of important recipes on multi-threading and parallel programming
  • This book takes a close look at the Java 9 APIs and their impact on concurrency
  • See practical examples on thread safety, high-performance classes, safe sharing, and a whole lot more

Who This Book Is For

The book is for Java developers and programmers at an intermediate to advanced level. It will be especially useful for developers who want to take advantage of task-based recipes using Java 9’s concurrent API to program thread-safe solutions.

What You Will Learn

  • Find out to manage the basic components of the Java Concurrency API
  • Use synchronization mechanisms to avoid data race conditions and other problems of concurrent applications
  • Separate the thread management from the rest of the application with the Executor framework
  • Solve problems using a parallelized version of the divide and conquer paradigm with the Fork / Join framework
  • Process massive data sets in an optimized way using streams and reactive streams
  • See which data structures we can use in concurrent applications and how to use them
  • Practice efficient techniques to test concurrent applications
  • Get to know tips and tricks to design concurrent applications

In Detail

Writing concurrent and parallel programming applications is an integral skill for any Java programmer. Java 9 comes with a host of fantastic features, including significant performance improvements and new APIs.

This book will take you through all the new APIs, showing you how to build parallel and multi-threaded applications. The book covers all the elements of the Java Concurrency API, with essential recipes that will help you take advantage of the exciting new capabilities.

You will learn how to use parallel and reactive streams to process massive data sets. Next, you will move on to create streams and use all their intermediate and terminal operations to process big collections of data in a parallel and functional way.

Further, you’ll discover a whole range of recipes for almost everything, such as thread management, synchronization, executors, parallel and reactive streams, and many more. At the end of the book, you will learn how to obtain information about the status of some of the most useful components of the Java Concurrency API and how to test concurrent applications using different tools.

Style and approach

This recipe-based book will allow you to explore the exciting capabilities of concurrency in Java. After reading this book, you will be able to comfortably build parallel applications in Java 9.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Conventions
    6. Reader feedback
    7. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Thread Management
    1. Introduction
    2. Creating, running, and setting the characteristics of a thread
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Interrupting a thread
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Controlling the interruption of a thread
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Sleeping and resuming a thread
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Waiting for the finalization of a thread
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    7. Creating and running a daemon thread
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    8. Processing uncontrolled exceptions in a thread
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Using thread local variables
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    10. Grouping threads and processing uncontrolled exceptions in a group of threads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Creating threads through a factory
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  3. Basic Thread Synchronization
    1. Introduction
    2. Synchronizing a method
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Using conditions in synchronized code
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Synchronizing a block of code with a lock
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Avoiding deadlocks
      5. See also
    5. Synchronizing data access with read/write locks
      1. Getting ready...
      2. How to do it...
      3. How it works...
      4. See also
    6. Using multiple conditions in a lock
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Advanced locking with the StampedLock class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  4. Thread Synchronization Utilities
    1. Introduction
    2. Controlling concurrent access to one or more copies of a resource
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Fairness in semaphores
      5. See also
    3. Waiting for multiple concurrent events
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Synchronizing tasks in a common point
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Resetting a CyclicBarrier object
        2. Broken CyclicBarrier objects
      5. See also
    5. Running concurrent-phased tasks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Registering participants in Phaser
        2. Forcing the termination of Phaser
      5. See also
    6. Controlling phase change in concurrent-phased tasks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Exchanging data between concurrent tasks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    8. Completing and linking tasks asynchronously
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also...
  5. Thread Executors
    1. Introduction
    2. Creating a thread executor and controlling its rejected tasks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Executing tasks in an executor that returns a result
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Running multiple tasks and processing the first result
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Running multiple tasks and processing all the results
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Running a task in an executor after a delay
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Running a task in an executor periodically
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Canceling a task in an executor
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Controlling a task finishing in an executor
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Separating the launching of tasks and the processing of their results in an executor
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  6. Fork/Join Framework
    1. Introduction
    2. Creating a fork/join pool
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Joining the results of the tasks
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    4. Running tasks asynchronously
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    5. Throwing exceptions in the tasks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Canceling a task
      1. Getting ready...
      2. How to do it...
      3. How it works...
      4. See also
  7. Parallel and Reactive Streams
    1. Introduction
    2. Creating streams from different sources
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Reducing the elements of a stream
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Collecting the elements of a stream
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Applying an action to every element of a stream
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Filtering the elements of a stream
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Transforming the elements of a stream
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Sorting the elements of a stream
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Verifying conditions in the elements of a stream
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Reactive programming with reactive streams
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
  8. Concurrent Collections
    1. Introduction
    2. Using non-blocking thread-safe deques
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. Using blocking thread-safe deques
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Using blocking thread-safe queue ordered by priority
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Using thread-safe lists with delayed elements
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Using thread-safe navigable maps
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Using thread-safe HashMaps
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Using atomic variables
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Using atomic arrays
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Using the volatile keyword
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    11. Using variable handles
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  9. Customizing Concurrency Classes
    1. Introduction
    2. Customizing the ThreadPoolExecutor class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Implementing a priority-based Executor class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Implementing the ThreadFactory interface to generate custom threads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. Using our ThreadFactory in an Executor object
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Customizing tasks running in a scheduled thread pool
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Implementing the ThreadFactory interface to generate custom threads for the fork/join framework
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Customizing tasks running in the fork/join framework
      1. How to do it...
      2. How it works...
      3. See also
    9. Implementing a custom Lock class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Implementing a transfer queue-based on priorities
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Implementing your own atomic object
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    12. Implementing your own stream generator
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    13. Implementing your own asynchronous stream
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  10. Testing Concurrent Applications
    1. Introduction
    2. Monitoring a Lock interface
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Monitoring a Phaser class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Monitoring an Executor framework
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Monitoring a fork/join pool
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Monitoring a stream
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Writing effective log messages
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Analyzing concurrent code with FindBugs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Configuring Eclipse for debugging concurrency code
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Configuring NetBeans for debugging concurrency code
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    11. Testing concurrency code with MultithreadedTC
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Monitoring with JConsole
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  11. Additional Information
    1. Introduction
    2. Processing results for Runnable objects in the Executor framework
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Processing uncontrolled exceptions in a ForkJoinPool class
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    4. Using a blocking thread-safe queue for communicating with producers and consumers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Monitoring a Thread class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Monitoring a Semaphore class
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Generating concurrent random numbers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  12. Concurrent Programming Design
    1. Introduction
    2. Using immutable objects when possible
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Avoiding deadlocks by ordering locks
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    4. Using atomic variables instead of synchronization
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Holding locks for as short time as possible
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Delegating the management of threads to executors
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Using concurrent data structures instead of programming yourself
      1. There's more...
      2. See also
    8. Taking precautions using lazy initialization
      1. Getting ready
      2. How to do it...
      3. How it works...
    9. Using the fork/join framework instead of executors
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Avoiding the use of blocking operations inside a lock
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Avoiding the use of deprecated methods
    12. Using executors instead of thread groups
      1. See also
    13. Using streams to process big data sets
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    14. Other tips and tricks
      1. See also