On this page
Scoped Values
Immutable data sharing across threads.
Overview
Immutable data sharing across threads.
Example
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
executor.submit(() -> System.out.println("Virtual thread"));
}
Common Use Cases
- High-throughput server applications
- Parallel data processing
Pitfalls to Avoid
- Pinning virtual threads with synchronized blocks on JDK 21
- Unbounded thread creation
Related Topics
- ExecutorService
- CompletableFuture
Best Practices
- Understand when to use scoped values versus simpler alternatives
- Write unit tests covering edge cases and failure paths
- Follow Java conventions and prefer standard library APIs when available
- Profile before optimizing — measure impact in your specific workload