前言 JDK21可以说是一个史诗级的产品,带来了目前Java高并发较为顶级解决方案虚拟线程 ,话不多说,直接来上代码体验一下功能,洛阳目前使用的笔记本是华为的matebook D15 2022款,16+512,cpu是i7-1195G7,idea2024.1.7,所有都是使用默认配置,话不多说,上代码(全部代码在最后) 正文 先是一个模拟IO操作的方法 // 模拟工作负载的方法 private static void simulateWork ( ) { try { // 模拟一个耗时100毫秒的I/O操作 Thread . sleep ( 100 ) ; } catch ( InterruptedException e) { // 如果线程被中断,重新设置中断状态 Thread . currentThread ( ) . interrupt ( ) ; } } 使用虚拟线程的测试代码 // 使用虚拟线程执行任务的方法 private static long executeWithVirtualThreads ( int taskCount) throws InterruptedException { // 记录开始时间 long start= System . currentTimeMillis ( ) ; // 创建一个虚拟线程执行器 try ( ExecutorService executor= Executors . newVirtualThreadPerTaskExecutor ( ) ) { // 提交taskCount个任务到虚拟线程执行器 for ( int i= 0 ; i< taskCount; i++ ) { executor. submit ( Test :: simulateWork ) ; } // 关闭执行器,不再接受新任务 executor. shutdown ( ) ; // 等待所有任务完成,最多等待1小时 executor. awaitTermination ( 1 , TimeUnit . HOURS) ; } // 计算并返回总耗时 return System . currentTimeMillis ( ) - start; } 使用线程池的测试代码 // 使用传统线程执行任务的方法 private static long executeWithTraditionalThreads ( int taskCount) throws InterruptedException { // 记录开始时间 long start= System . currentTimeMillis ( ) ; // 创建一个固定大小为100的线程池 try ( ExecutorService executor= Executors . newFixedThreadPool ( 100 ) ) { // 提交taskCount个任务到线程池 for ( int i= 0 ; i< taskCount; i++ ) { executor. submit ( Test :: simulateWork ) ; } // 关闭线程池,不再接受新任务 executor. shutdown ( ) ; // 等待所有任务完成,最多等待1小时 executor. awaitTermination ( 1 , TimeUnit . HOURS) ; } // 计算并返回总耗时 return System . currentTimeMillis ( ) - start; } 格式化时间(可有可无) // 格式化时间的方法,将毫秒转换为更易读的格式 private static String formatTime ( long milliseconds) { // 返回格式化的字符串,同时显示毫秒数和秒数(保留两位小数) return String . format ( "%d 毫秒 (%.2f 秒)" , milliseconds, milliseconds/ 1000.0 ) ; } main方法 public static void main ( String [ ] args) throws InterruptedException { // 设置要执行的任务数量 int taskCount= 100000 ; // 开始测试传统线程 System . out. println ( "正在使用传统线程执行任务..." ) ; // 执行传统线程测试并记录耗时 long traditionalTime= executeWithTraditionalThreads ( taskCount) ; // 输出传统线程的执行时间 System . out. println ( "传统线程完成任务,耗时 " + formatTime ( traditionalTime) ) ; // 开始测试虚拟线程 System . out. println ( "\n正在使用虚拟线程执行任务..." ) ; // 执行虚拟线程测试并记录耗时 long virtualTime= executeWithVirtualThreads ( taskCount) ; // 输出虚拟线程的执行时间 System . out. println ( "虚拟线程完成任务,耗时 " + formatTime ( virtualTime) ) ; // 计算两种线程执行时间的差异 long timeDifference= traditionalTime- virtualTime; // 输出性能差异和速度提升倍数 System . out. println ( "\n性能差异:" + formatTime ( timeDifference) + " (" + String . format ( "%.2f" , ( double ) traditionalTime/ virtualTime) + " 倍速度提升)" ) ; } 运行结果 正在使用传统线程执行任务. . . 传统线程完成任务,耗时106725 毫秒( 106.73 秒) 正在使用虚拟线程执行任务. . . 虚拟线程完成任务,耗时2014 毫秒( 2.01 秒) 性能差异:104711 毫秒( 104.71 秒) ( 52.99 倍速度提升) Process finishedwith exit code0 可以看到,使用了虚拟线程之后,速度提升了50倍左右,至于底层原理之类的,那不是洛阳这种小垃圾去看的,洛阳只学会能够如何使用就可以了(手动狗头),以下是整个测试类的完整代码,大家可以自己跑一下感受一下,但是虚拟线程也并不是说能够无脑上,要根据实际情况考虑,正所谓没有最好的架构、只有最合适的架构。 package com. travel. echo. test ; import java. util. concurrent. ExecutorService ; import java. util. concurrent. Executors ; import java. util. concurrent. TimeUnit ; /** * @Description * @Author LuoYang * @Date 2025/12/6 周六 */ public class Test { public static void main ( String [ ] args) throws InterruptedException { // 设置要执行的任务数量 int taskCount= 100000 ; // 开始测试传统线程 System . out. println ( "正在使用传统线程执行任务..." ) ; // 执行传统线程测试并记录耗时 long traditionalTime= executeWithTraditionalThreads ( taskCount) ; // 输出传统线程的执行时间 System . out. println ( "传统线程完成任务,耗时 " + formatTime ( traditionalTime) ) ; // 开始测试虚拟线程 System . out. println ( "\n正在使用虚拟线程执行任务..." ) ; // 执行虚拟线程测试并记录耗时 long virtualTime= executeWithVirtualThreads ( taskCount) ; // 输出虚拟线程的执行时间 System . out. println ( "虚拟线程完成任务,耗时 " + formatTime ( virtualTime) ) ; // 计算两种线程执行时间的差异 long timeDifference= traditionalTime- virtualTime; // 输出性能差异和速度提升倍数 System . out. println ( "\n性能差异:" + formatTime ( timeDifference) + " (" + String . format ( "%.2f" , ( double ) traditionalTime/ virtualTime) + " 倍速度提升)" ) ; } // 使用传统线程执行任务的方法 private static long executeWithTraditionalThreads ( int taskCount) throws InterruptedException { // 记录开始时间 long start= System . currentTimeMillis ( ) ; // 创建一个固定大小为100的线程池 try ( ExecutorService executor= Executors . newFixedThreadPool ( 100 ) ) { // 提交taskCount个任务到线程池 for ( int i= 0 ; i< taskCount; i++ ) { executor. submit ( Test :: simulateWork ) ; } // 关闭线程池,不再接受新任务 executor. shutdown ( ) ; // 等待所有任务完成,最多等待1小时 executor. awaitTermination ( 1 , TimeUnit . HOURS) ; } // 计算并返回总耗时 return System . currentTimeMillis ( ) - start; } // 使用虚拟线程执行任务的方法 private static long executeWithVirtualThreads ( int taskCount) throws InterruptedException { // 记录开始时间 long start= System . currentTimeMillis ( ) ; // 创建一个虚拟线程执行器 try ( ExecutorService executor= Executors . newVirtualThreadPerTaskExecutor ( ) ) { // 提交taskCount个任务到虚拟线程执行器 for ( int i= 0 ; i< taskCount; i++ ) { executor. submit ( Test :: simulateWork ) ; } // 关闭执行器,不再接受新任务 executor. shutdown ( ) ; // 等待所有任务完成,最多等待1小时 executor. awaitTermination ( 1 , TimeUnit . HOURS) ; } // 计算并返回总耗时 return System . currentTimeMillis ( ) - start; } // 模拟工作负载的方法 private static void simulateWork ( ) { try { // 模拟一个耗时100毫秒的I/O操作 Thread . sleep ( 100 ) ; } catch ( InterruptedException e) { // 如果线程被中断,重新设置中断状态 Thread . currentThread ( ) . interrupt ( ) ; } } // 格式化时间的方法,将毫秒转换为更易读的格式 private static String formatTime ( long milliseconds) { // 返回格式化的字符串,同时显示毫秒数和秒数(保留两位小数) return String . format ( "%d 毫秒 (%.2f 秒)" , milliseconds, milliseconds/ 1000.0 ) ; } } 结尾