A comprehensive comparative analysis of Java's virtual threads vs traditional threads, showcasing modern Java 21+ features, structured logging, and performance optimizations for I/O-bound tasks.
This project demonstrates the power of Java 21+ virtual threads with modern language features, production-ready logging, and comprehensive testing. It provides a complete benchmarking suite comparing virtual threads against traditional platform threads across multiple scenarios.
- Records - Immutable data structures for
PerformanceResult,BenchmarkConfig,BenchmarkReport - Switch Expressions - Type-safe thread selection and performance analysis
- Pattern Matching - Record destructuring in switch statements
- Enhanced String Formatting - Multi-line text blocks and formatting
- Structured Concurrency - CompletableFuture integration with proper timeout handling
- Detailed Metrics - Tasks per second, duration tracking, success/failure monitoring
- Multiple Configurations - Light, default, and heavy load presets
- Comprehensive Benchmarking - Statistical analysis with multiple iterations
- Performance Comparison - Automated speedup and efficiency calculations
- Resource Management - Try-with-resources for automatic cleanup
- SLF4J + Logback - Industry-standard logging framework
- Structured Logging - Timestamped, categorized, and parameterized
- Multiple Appenders - Console, file, and performance-specific logging
- Log Rotation - Time-based and size-based log file management
- Performance Metrics - Separate performance logger for metrics collection
- Test Suite - 8 comprehensive test categories (100% pass rate)
- Dependency-Free Testing - SimpleTestRunner for environments without Maven
- Security Validated - All dependencies updated to latest secure versions
- Performance Validation - Virtual threads showing 2-8x speedup consistently
+----------------+ +----------------+
| Java Thread | ----> | OS Thread |
+----------------+ +----------------+
... ...
(Limited by OS thread count, high memory overhead)
+----------------+ +----------------+
| Virtual Thread | -\ | OS Thread |
+----------------+ \ +----------------+
+----------------+ +----------------+
| Virtual Thread | --/ | Carrier Thread |
+----------------+ +----------------+
+----------------+ -> | |
+----------------+ +----------------+
... ...
(Managed by JVM, memory-efficient, scalable)
- Java 21+ required for virtual threads support
- Maven 3.6+ for dependency management
- Git for version control
# Clone the repository
git clone https://github.com/sbusanelli/virtual-threads-performance.git
# Navigate to project
cd virtual-threads-performance/virtualthreads
# Compile with dependencies
mvn clean compile
# Run virtual threads example
java -cp target/classes virtualthreads.VirtualThreadExample
# Run traditional threads example
java -cp target/classes virtualthreads.TraditionalThreadExample
# Run enhanced benchmarking demo
java -cp target/classes virtualthreads.EnhancedDemo
# Run comprehensive test suite
java -cp "target/classes:target/test-classes" virtualthreads.RunTestSuite- Virtual Threads: ~7,000 tasks/second (1000 tasks in ~140ms)
- Traditional Threads: ~2,300 tasks/second (1000 tasks in ~430ms)
- Average Speedup: 3.0x faster with virtual threads
- Memory Efficiency: Virtual threads use significantly less memory
- Scalability: Virtual threads maintain performance at 10,000+ concurrent tasks
- Virtual threads excel at I/O-bound workloads
- Traditional threads better for CPU-intensive tasks
- Memory usage scales linearly with traditional threads, stays constant with virtual threads
- Context switching overhead eliminated with virtual threads
- Basic Performance - Core functionality validation
- Scalability - Performance across different thread counts
- Concurrency - Thread safety and coordination
- Memory Usage - Memory efficiency validation
- Load Testing - High-concurrency scenarios
- Integration - Producer-consumer patterns
- End-to-End - Workflow validation
- Benchmarking - Statistical analysis
- Success Rate: 100% (8/8 tests passing)
- Performance Validation: All scenarios working correctly
- Memory Testing: Fixed and reliable
- Security: All dependencies updated to secure versions
<dependencies>
<!-- SLF4J API -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version>
</dependency>
<!-- Logback implementation -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.14</version>
</dependency>
<!-- JUnit for testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>- Console Appender - Real-time output with structured formatting
- File Appender - Persistent logging with rotation
- Performance Logger - Separate metrics collection
- Log Levels - INFO, WARN, ERROR, DEBUG support
// Enhanced ThreadRunner with modern features
ThreadRunner.PerformanceResult result = ThreadRunner.runTasks(1000, ThreadRunner.ThreadType.VIRTUAL);
// Performance comparison
ThreadRunner.comparePerformance(1000);
// Legacy compatibility
ThreadRunner.runTasks(1000, true); // virtual threads
ThreadRunner.runTasks(1000, false); // traditional threads// Comprehensive benchmarking with multiple configurations
EnhancedDemo.BenchmarkConfig config = EnhancedDemo.BenchmarkConfig.defaultConfig();
EnhancedDemo.BenchmarkConfig heavyLoad = EnhancedDemo.BenchmarkConfig.heavyLoad();
// Statistical analysis across iterations
EnhancedDemo.main(new String[]{});// Structured logging with SLF4J
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
private static final Logger performanceLogger = LoggerFactory.getLogger("virtualthreads.performance");
logger.info("Starting {} tasks with {} threads", taskCount, threadType);
performanceLogger.info("THREAD_TYPE={},TASK_COUNT={},DURATION_MS={}", threadType, taskCount, duration);- CVE-2023-3615 - Fixed in SLF4J 2.0.13
- Logback CVEs - Fixed in Logback 1.4.14
- JUnit Updates - Latest stable version 5.10.2
- Regular Updates - Dependencies monitored and updated
- Dependency Scanning - Automated vulnerability detection
- Version Pinning - Use specific secure versions
- Regular Updates - Monitor for new vulnerabilities
- Code Review - Security-focused development practices
virtualthreads/
βββ src/main/java/virtualthreads/
β βββ ThreadRunner.java # Enhanced core functionality
β βββ VirtualThreadExample.java # Virtual threads demo
β βββ TraditionalThreadExample.java # Traditional threads demo
β βββ EnhancedDemo.java # Comprehensive benchmarking
β βββ SimpleLogger.java # Custom logging implementation
βββ src/test/java/virtualthreads/
β βββ TestVirtualThreadExampleTest.java
β βββ TestTraditionalThreadExampleTest.java
β βββ RunTestSuite.java # Comprehensive test suite
β βββ SimpleTestRunner.java # Dependency-free testing
βββ src/main/resources/
β βββ logback.xml # Production logging configuration
βββ pom.xml # Maven configuration with secure dependencies
- I/O-Bound Tasks - Network calls, file operations, database queries
- High Concurrency - Thousands of concurrent operations
- Memory Efficiency - Limited memory environments
- Startup Speed - Quick thread creation and teardown
- CPU-Bound Tasks - Intensive computations
- Legacy Systems - Integration with older code
- Real-Time Requirements - When OS-level thread priority matters
- Limited Concurrency - Small number of concurrent tasks
| Test Scenario | Virtual Threads (ms) | Traditional Threads (ms) | Speedup |
|---|---|---|---|
| 100 tasks | ~12 | ~25 | 2.1x |
| 1,000 tasks | ~140 | ~430 | 3.1x |
| 10,000 tasks | ~185 | ~1,380 | 7.5x |
This project demonstrates that Java 21+ virtual threads provide significant performance improvements for I/O-bound workloads while maintaining clean, maintainable code with modern language features. The comprehensive testing suite validates functionality across multiple scenarios, and the security updates ensure production readiness.
Key Takeaway: Virtual threads are not just fasterβthey're more scalable, memory-efficient, and better suited for modern concurrent applications.