【JDK21】初体验

IDEA 2023.2.2已支持JDK21

Java 21发布,IntelliJ IDEA 2023.2.2已完美支持。

想要开发Java 21代码的开发者可以升级了!

Java新特性

Java 9 - 21:新特性解读

虚拟线程

虚拟线程创建

(1)使用静态构建器方法

(2)使用Thread.ofVirtual()

(3)使用虚拟线程工厂

(4)与ExecutorService结合使用

java 复制代码
public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            System.out.println("Hello, jdk21");
        };
        // 使用静态构建器方法
        Thread.startVirtualThread(runnable);
        // 使用Thread.ofVirtual()
        Thread.ofVirtual().name("jdk21-virtual-thread").start(runnable);
        // 使用虚拟线程工厂
        ThreadFactory virtualThreadFactory = Thread.ofVirtual().name("jdk21", 0).factory();
        Thread factoryThread = virtualThreadFactory.newThread(runnable);
        factoryThread.start();
        // 与ExecutorService结合使用
        try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor()) {
            for (int i = 0; i < 100; i++) {
                executorService.submit(runnable);
            }
        }
        ExecutorService executorService = Executors.newThreadPerTaskExecutor(virtualThreadFactory);
        for (int i = 0; i < 10; i++) {
            executorService.submit(() -> {
                System.out.println(Thread.currentThread().getName() + " is running...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " is done.");
            });
        }
        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.MINUTES);
    }

虚拟线程测试

java 复制代码
public static void main(String[] args) {
        int timesCount = 1000;
        long l1 = System.currentTimeMillis();
        for (int i = 0; i < timesCount; i++) {
            int finalI = i;
            Thread.ofPlatform().name("平台线程").start(() -> {
                System.out.println("线程名称:" + Thread.currentThread().getName() + "线程ID" + Thread.currentThread().threadId() + "执行第" + finalI + "个平台线程");
            });
        }
        long l2 = System.currentTimeMillis();
        for (int i = 0; i < timesCount; i++) {
            int finalI = i;
            Thread.ofVirtual().name("虚拟线程").start(() -> {
                System.out.println("线程名称:" + Thread.currentThread().getName() + "线程ID" + Thread.currentThread().threadId() + "执行第" + finalI + "个平台线程");
            });
        }
        System.out.println("线程分别执行 " + timesCount + " 次压测...");
        System.out.println("平台线程执行耗时:" + (System.currentTimeMillis() - l1) + "毫秒");
        System.out.println("虚拟线程执行耗时:" + (System.currentTimeMillis() - l2) + "毫秒");
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
相关推荐
小梦爱安全几秒前
Ansible剧本1
java·网络·ansible
pupudawang24 分钟前
Spring Boot 热部署
java·spring boot·后端
我登哥MVP26 分钟前
【SpringMVC笔记】 - 9 - 异常处理器
java·spring boot·spring·servlet·tomcat·maven
下地种菜小叶32 分钟前
Spring Boot 2.x 升级 3.x / 4.x 怎么做?一次讲清 JDK、Jakarta、依赖兼容与上线策略
java·spring boot·后端
iiiiyu34 分钟前
常用API(StringJoiner类 & Math类 & System类)
java·大数据·开发语言·数据结构·编程语言
有梦想的小何1 小时前
`Java并发排障实录:没有报错,却把正确数据覆盖错了`
java·spring boot·mysql·spring cloud
Xiu Yan1 小时前
Java 转 C++ 系列:函数对象、谓词和内建函数对象
java·开发语言·c++
那个失眠的夜1 小时前
Spring整合Mybatis实现用户的CRUD
java·spring·mybatis
superantwmhsxx1 小时前
Spring Initializr创建springboot项目,提示java 错误 无效的源发行版:16
java·spring boot·spring
山河梧念1 小时前
【保姆级教程】VMware虚拟机安装全流程
android·java·数据库