【问题】jdk20执行虚拟线程报错解决方案

博客主要围绕Java的ofPlatform()预览API展开,该API默认禁用,需使用 --enable-preview 启用。还提及在Idea中增加配置、编辑配置添加jvm参数、指定JDK20来执行,以及Maven install报错提示同样是此API默认禁用的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java: ofPlatform() 是预览 API,默认情况下处于禁用状态。
  (请使用 --enable-preview 以启用预览 API)

IDEA启动步骤
步骤一
idea 设置 - 构建执行部署 - 编译器 - Java编译器 - 模块 - 找到对应的项目 - 编译选项 改为如下
-parameters --enable-preview

步骤二
JAVA虚拟机选项增加
--enable-preview

在idea增加配置

编辑配置 增加jvm参数

然后去执行

 public static void main(String[] args) {

        int 次数 = 100000;
        
        System.out.println("线程分别执行 "+次数+" 次压测...");
        
        long l1 = System.currentTimeMillis();
        for (int i = 0; i < 次数; i++) {
            int finalI = i;
            Thread.ofPlatform().name("平台线程").start(() -> {
//                System.out.println("线程名称:" + Thread.currentThread().getName() + "线程ID" + Thread.currentThread().threadId() + "执行第" + finalI +"个平台线程");
            });
        }
        System.out.println("平台线程执行耗时:" + (System.currentTimeMillis() - l1) + "毫秒");

        long l2 = System.currentTimeMillis();
        for (int i = 0; i < 次数; i++) {
            int finalI = i;
            Thread.ofVirtual().name("虚拟线程").start(() -> {
//                System.out.println("线程名称:" + Thread.currentThread().getName() + "线程ID" + Thread.currentThread().threadId() + "执行第" + finalI +"个平台线程");
            });
        }
        System.out.println("虚拟线程执行耗时:" + (System.currentTimeMillis() - l2) + "毫秒");
        
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

指定jdk20

   <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>20</source>
                    <target>20</target>
                        <compilerArgs>
                            <arg>--enable-preview</arg>
                        </compilerArgs>
                </configuration>
            </plugin>

Maven install报错提示

java: ofPlatform() 是预览 API,默认情况下处于禁用状态。
  (请使用 --enable-preview 以启用预览 API)

   <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>20</source>
                    <target>20</target>
                        <compilerArgs>
                            <arg>--enable-preview</arg>
                        </compilerArgs>
                </configuration>
            </plugin>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是Superman丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值