Feature #2224 : Apply virtual thread #2335
Open
+7,746
−196
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
issue #2224
Add Virtual Thread Support to Resilience4j
Summary
This PR introduces comprehensive support for Java 21's Virtual Threads (Project Loom) across all Resilience4j modules, enabling high-performance concurrent operations with significantly reduced resource overhead compared to traditional platform threads.
What Changed
Core Infrastructure
ExecutorServiceFactory: Central factory for thread management with automatic virtual/platform thread detectionresilience4j.thread.typesystem property to control thread type (virtualorplatform)Module Updates
FixedThreadPoolBulkheadnow supports virtual thread pools with proper naming conventionsSchedulerFactorywith thread-safe virtual thread support and automatic configuration detectionSemaphoreBasedRateLimiterimplementation for virtual thread compatibilityConcurrentHashMap → Smart CompletableFuture Usage
Applied selective CompletableFuture optimization based on operation complexity to prevent virtual thread pinning while maintaining performance:
✅ CompletableFuture Applied (Complex Operations)
InMemoryRegistryStore.entryMap: Uses optimizedputIfAbsentpattern with complex object creation viamappingFunction.apply()Key Optimizations Applied:
computeIfAbsentwith more efficientputIfAbsentfor better virtual thread performancecompleteExceptionally()returnstrueto prevent race conditionscomputeIfAbsentinternallyResult: Optimized memory usage and CPU performance while maintaining virtual thread benefits where they provide genuine value.
Performance Test Results: (with I/O 500-1500ms delays)
- Added a hash collision situation to the scenario
*Limited by thread pinning issues
**Platform threads limited to 20 due to high memory overhead (~2MB per thread vs ~1KB for virtual threads)
Testing Strategy
ThreadModeTestBaseSpring Boot Integration
Resilience4jThreadAutoConfigurationfor seamless Spring Boot integrationThreadMetricsAutoConfigurationwith Micrometer integration for monitoringDocumentation
Key Features
Performance Benefits
Configuration Options
System Property
Spring Boot Configuration
Environment Variable
export resilience4j.thread.type=virtualThread Naming Conventions
bulkhead-{name}-{number}(e.g.,bulkhead-test-1)bulkhead-{name}-v-{number}(e.g.,bulkhead-test-v-0)Technical Details
ExecutorServiceFactory Design
Thread Pool Behavior
ContextPropagatorfor virtual thread compatibilityTesting Architecture
Compatibility
Requirements
Migration Path
Testing Coverage
Enhanced Existing Tests
Breaking Changes
None - This is a fully backward-compatible addition. Existing applications will continue to work unchanged.
Future Considerations