Virtual Threads, Structured Concurrency, and Scoped Values: Explore Java’s New Threading Model

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Discover the exciting new APIs introduced by Java’s Project Loom. This quick guide covers all the essential concepts the Java developer needs in order to understand virtual threads, structured concurrency, and scoped values. Authors Ron Veen and David Vlijmincx clearly explain the superiority of virtual threads, introduced in Java 21, over the traditional Java threads of the past 25 years. Then they dive into how structured concurrency streamlines interactions by treating related threads as a unified unit of work, akin to the impact of structured programming on regular programming. From there, the authors explore the third Project Loom API and scoped values, clarifying their advantages over regular ThreadLocals and providing efficient usage insights. The book concludes with an exploration of common scheduling strategies. By the end of this book, you will have a solid understanding of the concepts behind Project Loom and will be able to immediately apply this knowledge to your own code. You Will Discover what virtual threads are, why they were created, and how to use them in your code Gain insight into how concurrency improves the observability of code Write your own StructuredTaskScopes tailored to your needs Understand why you should replace ThreadLocals with Scoped Values Know the different strategies for multithreaded computer programs Who This Book is For Java developers of any level. This knowledge is so fundamental to staying relevant in the Java job market that every developer needs to understand it for their career development.

Author(s): Ron Veen, David Vlijmincx
Edition: 1
Publisher: Apress
Year: 2024

Language: English
Commentary: Publisher PDF | Published: 19 September 2024 | This PDF has bookmarks and a working TOC. The PDF @ 22D2E10657E5B1C682DAB9732ED658A9 lacks the mentioned attributes.
Pages: xiii, 79
City: Berkeley, CA
Tags: Project Loom; Virtual Threads; Java; Structured Concurrency; Concurrency Models; Concurrency; Java 21

Table of Contents
About the Authors
About the Technical Reviewer
Introduction
Chapter 1: Virtual Threads
Existing Threading Model
Using Threads
Using the Executor Service
Async Programming
Virtual Threads
Carrier Threads
Mounting and Unmounting
Pinned Virtual Threads
When Not to Use Virtual Threads
Summary
Chapter 2: Structured Concurrency
What Is Structured Concurrency?
Unstructured Concurrency
Structured Concurrency
StructuredTaskScope
Basic Example of StructuredTaskScope
Subtasks
Using Virtual Threads by Default
Handling Timeouts
Structured ConcurrencyPolicies
The Invoke All Pattern
The Invoke Any Pattern
Implementing Our Own Strategy
Structured Concurrency Alternatives
Completable Futures
Reactive Programming
Summary
Chapter 3: Scoped Values
What Are ThreadLocal Variables Again?
How ThreadLocals Work
How ThreadLocals Work Internally
Advantages of ThreadLocals
Thread Safety
Storing Contextual Information
Thread-Specific Configuration
Disadvantages of ThreadLocals
Mutable State
Memory Leaks
ThreadLocals Everywhere
Introducing Scoped Values
A Simple ScopedValue Example
Supplying More Than One ScopedValue
When No Value Is Bound
Summing Up Scoped Values
Scope Binding Revisited
Standard Binding
Rebinding a ScopedValue
Final Thoughts
Summary
Chapter 4: Concurrency Patterns
How to Reason About Virtual Threads
How to Use Virtual Threads in Your Application
Virtual Threads for Web Applications
Using CompletableFuture with Virtual Threads
Summary
Index