On this page
ThreadLocal
Per-thread state management.
Overview
Per-thread state management.
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 threadlocal 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