悦之 發表於 2026-4-3 15:58:00

Java虚拟线程实战:Project Loom让并发编程更简单

<h2>前言</h2>
<p>Java 21 正式引入虚拟线程(Virtual Threads),这是 Project Loom 的核心成果。虚拟线程让 Java 并发编程从"难"变"简",彻底改变了高并发应用的开发方式。本文带你全面掌握虚拟线程。</p>
<h2>一、虚拟线程 vs 平台线程</h2>
<ul>
<li><strong>平台线程</strong>:对应操作系统线程,创建成本高(约1MB栈内存),数量受限</li>
<li><strong>虚拟线程</strong>:JVM 管理的轻量级线程,创建成本极低(约几KB),可创建数百万个</li>
</ul>
<pre><code>// 平台线程:创建100万个会OOM
Thread platformThread = new Thread(() -&gt; doWork());
platformThread.start();

// 虚拟线程:创建100万个轻松搞定
Thread virtualThread = Thread.ofVirtual().start(() -&gt; doWork());</code></pre>
<h2>二、创建虚拟线程的四种方式</h2>
<pre><code>// 方式1:Thread.ofVirtual()
Thread vt1 = Thread.ofVirtual().name("vt-1").start(() -&gt; {
    System.out.println("Virtual thread: " + Thread.currentThread());
});

// 方式2:Thread.startVirtualThread()
Thread vt2 = Thread.startVirtualThread(() -&gt; {
    System.out.println("Running in virtual thread");
});

// 方式3:虚拟线程 ExecutorService
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
    executor.submit(() -&gt; processRequest());
    executor.submit(() -&gt; processRequest());
}

// 方式4:ThreadFactory
ThreadFactory factory = Thread.ofVirtual().factory();
Thread vt4 = factory.newThread(() -&gt; doWork());
vt4.start();</code></pre>
<h2>三、实战:高并发 HTTP 请求处理</h2>
<pre><code>// 传统方式:线程池,受限于线程数
ExecutorService threadPool = Executors.newFixedThreadPool(200);

// 虚拟线程方式:每个请求一个虚拟线程
@RestController
public class UserController {

    @GetMapping("/users/{id}")
    public User getUser(@PathVariable Long id) {
      // 虚拟线程会在 I/O 阻塞时自动挂起,释放载体线程
      return userService.findById(id);// 数据库查询
    }
}

// Spring Boot 3.2+ 开启虚拟线程
// application.yml
// spring:
//   threads:
//   virtual:
//       enabled: true</code></pre>
<h2>四、性能对比测试</h2>
<pre><code>public class PerformanceTest {

    // 模拟 I/O 密集型任务
    static void ioTask() throws InterruptedException {
      Thread.sleep(100);// 模拟数据库查询
    }

    public static void main(String[] args) throws Exception {
      int taskCount = 10_000;

      // 平台线程池
      long start1 = System.currentTimeMillis();
      try (ExecutorService pool = Executors.newFixedThreadPool(200)) {
            List&lt;Future&lt;?&gt;&gt; futures = new ArrayList&lt;&gt;();
            for (int i = 0; i &lt; taskCount; i++) {
                futures.add(pool.submit(() -&gt; { ioTask(); return null; }));
            }
            for (Future&lt;?&gt; f : futures) f.get();
      }
      System.out.println("Platform threads: " + (System.currentTimeMillis() - start1) + "ms");
      // 输出约:5200ms

      // 虚拟线程
      long start2 = System.currentTimeMillis();
      try (ExecutorService vExecutor = Executors.newVirtualThreadPerTaskExecutor()) {
            List&lt;Future&lt;?&gt;&gt; futures = new ArrayList&lt;&gt;();
            for (int i = 0; i &lt; taskCount; i++) {
                futures.add(vExecutor.submit(() -&gt; { ioTask(); return null; }));
            }
            for (Future&lt;?&gt; f : futures) f.get();
      }
      System.out.println("Virtual threads: " + (System.currentTimeMillis() - start2) + "ms");
      // 输出约:120ms,提升40倍!
    }
}</code></pre>
<h2>五、注意事项</h2>
<ol>
<li><strong>避免 synchronized 块</strong>:会导致虚拟线程固定(pinning),改用 ReentrantLock</li>
<li><strong>不适合 CPU 密集型任务</strong>:虚拟线程优势在 I/O 密集型场景</li>
<li><strong>ThreadLocal 谨慎使用</strong>:大量虚拟线程时内存占用可能增加</li>
</ol>
<pre><code>// 错误:synchronized 导致 pinning
synchronized (lock) {
    Thread.sleep(100);// 虚拟线程被固定,无法挂起
}

// 正确:使用 ReentrantLock
ReentrantLock lock = new ReentrantLock();
lock.lock();
try {
    Thread.sleep(100);// 虚拟线程可以正常挂起
} finally {
    lock.unlock();
}</code></pre>
<h2>总结</h2>
<p>虚拟线程是 Java 并发编程的重大突破,特别适合 I/O 密集型的微服务场景。升级到 Java 21 + Spring Boot 3.2,一行配置即可享受虚拟线程带来的性能提升。</p>
<p>觉得有帮助请点赞收藏!有问题欢迎评论区交流 🚀</p>

</div>
<div id="MySignature" role="contentinfo">
   

---

📌 **如果觉得文章对你有帮助,欢迎点赞👍收藏⭐!**

💬 有问题或建议?欢迎在评论区留言讨论~

🔗 更多技术干货请关注作者:弥烟袅绕

📚 本文地址:https://www.cnblogs.com/czlws/p/19817808/java-virtual-threads-project-loom-concurrency<br><br>
来源:https://www.cnblogs.com/czlws/p/19817808/java-virtual-threads-project-loom-concurrency
頁: [1]
查看完整版本: Java虚拟线程实战:Project Loom让并发编程更简单