Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

👤 About the Instructor

Ayan Dutta - Instructor

Hi, I'm Ayan Dutta, a Software Architect, Instructor, and Content Creator.
I create practical, hands-on courses on Java, Spring Boot, Debugging, Git, Python, and more.


🌐 Connect With Me


📺 Subscribe on YouTube

YouTube


📚 Explore My Udemy Courses

🧩 Java Debugging Courses with Eclipse, IntelliJ IDEA, and VS Code


Eclipse Debugging Techniques

Java Debugging With IntelliJ

Java Debugging with VS Code

💡 Java Productivity & Patterns


IntelliJ IDEA Tips & Tricks

Creational Design Patterns

🐍 Python Debugging Courses


Python Debugging With PyCharm

Python Debugging with VS Code

Python Debugging (Free)

🛠 Git & GitHub Courses


GitHub Desktop Guide

Git & GitHub with Eclipse

📦 Virtual Threads & Structured Concurrency Repository

This repository contains all the demo code for the Udemy course: Java Virtual Threads & Structured Concurrency w/ Spring Boot

⚙️ Requirements

  • JDK 21+ (stable virtual threads)
  • JDK 19/20 also supported with --enable-preview
  • IntelliJ IDEA or Eclipse (recommended)
  • Maven 3.9+ for building projects

📥 Getting Started

Clone the repository and navigate to the project directory:

git clone https://github.com/j2eeexpert2015/virtual-threads-structured-concurrency.git
cd virtual-threads-structured-concurrency

Build the project:

mvn clean compile

🔧 FIRST STEP: IntelliJ IDEA Setup

Add --add-exports (and preview where needed) at compile and runtime.

IntelliJ IDEA Configuration

1. Compile flags

Go to Settings → Build, Execution, Deployment → Compiler → Java Compiler → "Additional command line parameters":

--enable-preview --add-exports=java.base/jdk.internal.vm=ALL-UNNAMED

2. Run/Debug flags

Go to Run/Debug Configurations → your app → VM options:

--enable-preview --add-exports=java.base/jdk.internal.vm=ALL-UNNAMED

3. Project Settings

You should already have set:

  • Project SDK = Java 21
  • Language level = 21 (Preview) for both Project and Modules

Keep these settings as they are.


▶️ Running the Demos

From IntelliJ IDEA:

After completing the setup above, you can run any demo directly from IntelliJ.

From Command Line (Maven):

Run Basic Virtual Thread Demo:

mvn clean compile exec:java -Dexec.mainClass="com.example.virtualthreadcreation.BasicVirtualThreadCreation"

Run Virtual Thread Builder Demo:

mvn exec:java -Dexec.mainClass="com.example.virtualthreadcreation.VirtualThreadCreationWithBuilder"

Run Structured Concurrency Demo:

mvn exec:java -Dexec.mainClass="com.example.structuredconcurrency.ProductPageWithStructuredConcurrency"

Run Continuation Demo:

mvn exec:java -Dexec.mainClass="com.example.continuation.SimpleContinuationDemo" -Dexec.args="--add-exports java.base/jdk.internal.vm=ALL-UNNAMED --enable-preview"

🐞 Debugging Configuration

IntelliJ IDEA Debugging Setup:

For debugging jdk.internal.vm.Continuation and other internal APIs:

  1. Go to Run/Debug Configurations
  2. Click Modify options
  3. Select Add additional command line parameters
  4. Add the same VM options:
    --add-exports java.base/jdk.internal.vm=ALL-UNNAMED --enable-preview
    

This enables the IntelliJ debugger to step into Continuation methods and other internal JDK classes.

📁 Project Structure Overview

src/main/java/com/example/
├── virtualthreadcreation/     # Basic virtual thread creation examples
├── structuredconcurrency/     # Structured concurrency demonstrations  
├── scopedvalue/              # ScopedValue vs ThreadLocal examples
├── continuation/             # Low-level Continuation API examples
├── pinning/                 # Virtual thread pinning detection
├── mountandunmount/         # Thread mounting behavior
└── util/                   # Utility classes (JFR, profiling, etc.)

🔧 Key Demo Categories

🧵 Virtual Thread Creation

  • BasicVirtualThreadCreation.java - Simple VT creation patterns
  • VirtualThreadCreationWithBuilder.java - Builder API usage
  • VirtualThreadCreationWithThreadFactory.java - Factory patterns

🏗️ Structured Concurrency

  • ProductPageWithStructuredConcurrency.java - Real-world scenarios
  • ProductAvailabilityWithShutdownOnSuccess.java - Success patterns
  • StructuredVsUnstructuredDemo.java - Side-by-side comparison

🔄 Context Propagation

  • ScopedValueExample.java - Modern context handling
  • ThreadLocalInheritanceProblem.java - Legacy issues demonstration

⚡ Performance & Monitoring

  • VirtualThreadMountUnmount.java - Mount/unmount behavior
  • VirtualThreadPinning.java - Pinning detection and prevention
  • MillionVirtualThreads.java - Scalability demonstrations

📝 Important Notes

JDK Version Compatibility:

  • --enable-preview is only required for JDK 19/20
  • On JDK 21+, virtual threads are stable - you can omit --enable-preview unless using other preview features
  • Structured Concurrency requires --enable-preview on all JDK versions (still in preview as of JDK 21)

Internal API Access:

  • --add-exports java.base/jdk.internal.vm=ALL-UNNAMED is required for Continuation examples
  • This exports internal JDK APIs that are normally encapsulated by the module system
  • ⚠️ Warning: Internal APIs can change without notice - use only for learning purposes

🚨 Troubleshooting

Issue: IllegalAccessError with Continuation examples
Solution: Ensure VM options include --add-exports java.base/jdk.internal.vm=ALL-UNNAMED

Issue: ClassNotFoundException for preview features
Solution: Add --enable-preview flag and ensure using compatible JDK version

Issue: Maven exec plugin not finding main class
Solution: Run mvn clean compile first to ensure classes are built

🎯 Course Companion Repository

This repository is designed to complement the "Java Virtual Threads & Structured Concurrency w/ Spring Boot" Udemy course. Each demo corresponds to specific course sections and lectures for hands-on learning.

For production Spring Boot examples with monitoring and observability, check out the companion repository: sample-retail-app


Happy Learning! 🚀