Skip to content

sbusanelli/virtual-threads-performance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Virtual Threads Performance - Java 21+ Enhanced

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.

πŸš€ Overview

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.

✨ Key Features

πŸ—οΈ Modern Java 21+ Features

  • 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

πŸ“Š Enhanced Performance Measurement

  • 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

πŸ“ Production-Ready Logging

  • 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

πŸ§ͺ Comprehensive Testing

  • 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

πŸ—οΈ Architecture

Traditional Threads

+----------------+       +----------------+
| Java Thread    | ----> | OS Thread      |
+----------------+       +----------------+
       ...                    ...
(Limited by OS thread count, high memory overhead)

Virtual Threads

+----------------+       +----------------+
| Virtual Thread | -\    | OS Thread      |
+----------------+   \   +----------------+
+----------------+       +----------------+
| Virtual Thread | --/   | Carrier Thread |
+----------------+       +----------------+
+----------------+    -> |                |
+----------------+       +----------------+
       ...                    ...
(Managed by JVM, memory-efficient, scalable)

πŸš€ Quick Start

Prerequisites

  • Java 21+ required for virtual threads support
  • Maven 3.6+ for dependency management
  • Git for version control

Build and Run

# 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

πŸ“Š Performance Results

Latest Benchmarks

  • 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

Key Insights

  • 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

πŸ§ͺ Testing

Test Coverage

  1. Basic Performance - Core functionality validation
  2. Scalability - Performance across different thread counts
  3. Concurrency - Thread safety and coordination
  4. Memory Usage - Memory efficiency validation
  5. Load Testing - High-concurrency scenarios
  6. Integration - Producer-consumer patterns
  7. End-to-End - Workflow validation
  8. Benchmarking - Statistical analysis

Test Results

  • 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

πŸ”§ Configuration

Dependencies

<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>

Logging Configuration

  • 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

πŸ“ˆ Usage Examples

Basic Usage

// 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

Advanced Benchmarking

// 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[]{});

Logging Integration

// 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);

πŸ”’ Security

Vulnerability Management

  • 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

Security Best Practices

  • Dependency Scanning - Automated vulnerability detection
  • Version Pinning - Use specific secure versions
  • Regular Updates - Monitor for new vulnerabilities
  • Code Review - Security-focused development practices

πŸ“ Project Structure

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

πŸš€ Performance Benefits

When to Use Virtual Threads

  • 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

When to Use Traditional Threads

  • 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

πŸ“Š Benchmarking Results Summary

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

🎯 Conclusion

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.

About

πŸš€ Comprehensive performance comparison between Java Virtual Threads and Go Goroutines. Production-grade benchmark tool with real-time metrics, memory tracking, and scalability analysis for I/O-bound concurrent workloads.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages