Modern Concurrency in Java

Book description

Welcome to the future of Java. With this book, you'll explore the transformative world of Java 21's key feature: virtual threads. Remember struggling with the cost of thread creation, encountering limitations on scalability, and facing difficulties in achieving high throughput? Those days are over. This practical guide takes you from Java 1.0 to the cutting-edge advancements of Project Loom.

You'll learn more than just theory. Author A N M Bazlur Rahman equips you with a toolkit for taking real-world action. Take a deep dive into the intricacies of virtual threads and complex topics such as ForkJoinPool, continuation, rate limiting, debugging, and monitoring. You'll not only learn how they work, but you'll also pick up expert tips and tricks to help you master these concepts. And you'll learn about structured concurrency and scoped values—critical skills for building Java applications that are scalable and efficient.

  • Get an in-depth understanding of virtual threads
  • Understand the implementation of virtual thread internals
  • Gain performance improvement in blocking operations
  • Learn why structured concurrency is beneficial
  • Know where to use scoped value
  • Understand the relevance of reactive Java with the advent of virtual threads

A N M Bazlur Rahman is a software engineer with over a decade of experience in Java and related technologies. A speaker at various international conferences and Java user groups, his talks often focus on specialized topics such as concurrency and virtual threads.

Publisher resources

View/Submit Errata

Table of contents

  1. Brief Table of Contents (Not Yet Final)
  2. 1. Introduction
    1. A Brief History of Threads in Java
      1. Java Is Made of Threads
      2. Threads: The Backbone of the Java Platform
    2. The Genesis of Java 1.0 Threads
      1. Starting Threads
    3. Understanding the Hidden Costs of Threads
      1. How Many Threads Can You Create?
    4. Resource Efficiency in High-Scale Applications
      1. The Parallel Execution Strategy
      2. Introducing Executor Framework
      3. Remaining Challenges
    5. A Leap Towards Efficient Multithreading
      1. Cache Affinity and Task Distribution
      2. Work-Stealing Algorithm
      3. Bringing Composability into Play with CompletableFuture
    6. A Different Paradigm for Asynchronous Programming
      1. Drawbacks of Using Reactive Frameworks
    7. Revolutionizing Concurrency in Java
      1. The Promise of Virtual Threads
      2. Seamless Integration with Existing Codebases
      3. Virtual Threads and Platform Threads
      4. Intelligent Handling of Blocking Operations
      5. Benefits of Embracing Virtual Threads
    8. In Closing
  3. 2. Understanding Virtual Threads
    1. What is a Virtual Thread
      1. Key Differences from Classical Threads
      2. The Two Kinds of Threads in Java
    2. Setting Up Your Environment for Virtual Threads
      1. Creating Virtual Threads in Java
    3. Adapting to Virtual Threads
    4. Demonstrating Virtual Thread Creation in Java
      1. Throughput and Scalability
      2. The Fundamental Principle Behind Virtual Threads’ Scalability
      3. The Practical Implications
    5. How Virtual Threads Work Under the Hood
      1. Stack Frames and Memory Management
      2. Carrier Threads and OS Involvement
      3. Handling Blocking Operations
      4. Transparency and Invisibility
      5. Simplifying Asynchronous Operations
      6. The Promise of Structured Concurrency
    6. Managing Resource Constraints with Rate Limiting
      1. Understanding Semaphores in Java
      2. Why Use a Semaphore?
    7. Limitations of Virtual Threads
      1. Deeper Dive into Pinning
      2. Addressing the Pinning Problem with ReentrantLock
      3. Native Method Invocation and Pinning
    8. The Conundrum of ThreadLocal Variables in Virtual Threads
      1. Challenges with Virtual Threads
      2. Monitoring
      3. Monitoring ThreadLocals
      4. Monitoring Pinning
      5. Viewing Virtual Threads in jcmd Thread Dumps
    9. Generating Thread Dumps with HotSpotDiagnosticsMXBean
    10. Practical Tips for Migrating to Virtual Threads
      1. Reaffirming the Benefits of Virtual Threads
      2. It’s About Scalability
    11. In Closing
  4. 3. The Mechanics of Modern Concurrency in Java
    1. ThreadPool
    2. Why do we need a thread pool?
    3. Building a Simple Thread Pool in Java
    4. The Executor Framework
      1. FixedThreadPool
      2. CachedThreadPool
      3. SingleThreadExecutor
      4. ScheduledThreadPoolExecutor
      5. WorkStealingPool
      6. Callable and Future: Handling Task Results
    5. The ForkJoinPool
      1. Why ForkJoinPool for virtual threads
    6. Continuation
    7. Building Our Own Virtual Threads from Scratch
      1. The NanoThread Class
      2. The NanoThread Scheduler
      3. Simulating I/O Operations with File Transfer
      4. Putting It All Together
    8. Virtual Threads and I/O Polling
    9. In Closing
  5. 4. Structured Concurrency
    1. The Challenge of Unstructured Concurrency
    2. The Promise of Structured Concurrency
      1. Understanding the API
    3. StructuredTaskScope
    4. Scopes and Subtasks (Relationship and Lifecycle)
    5. Shutdown Policies
      1. ShutdownOnFailure
      2. ShutdownOnSuccess
    6. Concurrent Workflows (Fan-Out and Fan-In Scenarios)
      1. Fan-out Scenarios
      2. Fan-in Scenarios
    7. Custom Shutdown Policies
    8. Nested Scopes
      1. Observability
      2. In Closing
  6. About the Author

Product information

  • Title: Modern Concurrency in Java
  • Author(s): A N M Bazlur Rahman
  • Release date: September 2025
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098165413