You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JDK 21虚拟线程中Thread.sleep()导致后续LockSupport.parkNanos()无法正常阻塞的问题问询

JDK 21虚拟线程中Thread.sleep()导致后续LockSupport.parkNanos()无法正常阻塞的问题问询

大家好,我在测试JDK 21虚拟线程的LockSupport相关操作时遇到了一个诡异的问题,特来请教社区的大佬们:

环境

  • Java 21+(启用虚拟线程特性)

问题描述

当在虚拟线程中先调用Thread.sleep()后,后续的LockSupport.parkNanos()调用会出现提前返回的情况,无法正常阻塞指定时长。

我设计了如下对比测试:同时启动一个虚拟线程和一个平台线程,两个线程执行完全一致的逻辑:

  1. 先执行Thread.sleep(10)
  2. 连续调用两次LockSupport.parkNanos(9_000_000_000L)(即每次预期阻塞9秒)
  3. 主线程每隔1秒对两个线程执行一次LockSupport.unpark()操作

预期与实际行为

预期行为

两个线程每次迭代的总耗时都应该在2000ms左右——因为每次parkNanos()会被主线程的unpark打断(约1秒后),两次park操作加起来总耗时约2秒。

实际行为

  • ✅ 平台线程:表现符合预期,每次迭代耗时约2000ms
  • ❌ 虚拟线程:每次迭代仅耗时约1000ms,两次LockSupport.parkNanos()调用在首次Thread.sleep()后就全部提前返回,完全没有按预期阻塞

最小复现代码

public class Main {
    public static void main(String[] args) {
        Thread thread1 = Thread.startVirtualThread(() -> {
            while (true) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long startNanos_25_15 = System.nanoTime();
                LockSupport.parkNanos(9 * 1000 * 1000000L);
                LockSupport.parkNanos(9 * 1000 * 1000000L);
                long endNanos_25_17 = System.nanoTime();
                System.out.println("Virtual thread: " + ((endNanos_25_17 - startNanos_25_15) / 1000000.0));
            }
        });

        Thread thread2 = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long startNanos_25_15 = System.nanoTime();
                LockSupport.parkNanos(9 * 1000 * 1000000L);
                LockSupport.parkNanos(9 * 1000 * 1000000L);
                long endNanos_25_17 = System.nanoTime();
                System.out.println("Platform thread: " + ((endNanos_25_17 - startNanos_25_15) / 1000000.0));
            }
        });

        thread2.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        while (true) {
            LockSupport.unpark(thread1);
            LockSupport.unpark(thread2);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

控制台输出片段

Virtual thread: 985.0724
Virtual thread: 988.1296
Platform thread: 1988.9397
Virtual thread: 988.9953
Platform thread: 1989.7579
Virtual thread: 990.4098
Virtual thread: 988.9639
Virtual thread: 989.0095
Platform thread: 1991.151
Virtual thread: 989.9759
Platform thread: 1990.103
Virtual thread: 988.1343
Virtual thread: 990.1048
Platform thread: 1991.1685
Virtual thread: 989.0044
Virtual thread: 989.0884
Platform thread: 1991.3585
Virtual thread: 989.889
Virtual thread: 988.7816
Virtual thread: 990.0407

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 10:24:35