JDK21中的虚拟线程

193 阅读1分钟

前言

JDK21之后,提供了创建并启动虚拟线程的便捷方式

创建虚拟线程

JDK21之后,提供了Thread.startVirtualThread方法快速创建虚拟线程

public class VirThreadDemo {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = Thread.startVirtualThread(() -> {
            System.out.println("虚拟线程创建=============");
        });
        thread.join();

    }
}

输出结果为

image.png

总结

JDK21中最终确定虚拟线程,可以使用它快速创建并启动虚拟线程