• 【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结合使用

    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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    虚拟线程测试

    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();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    TVM神经编译器
    CSS常见用法 以及JS基础语法
    Python 函数进阶-高阶函数
    nn.KLDivLoss,nn.CrossEntropyLoss,nn.MSELoss,Focal_Loss
    Mathorcup数学建模竞赛第四届-【妈妈杯】B题:基于层次分析法与BP神经网络对书籍推荐的研究(附解析思路及MATLAB代码)
    独家巨献!阿里专家兼Github贡献者,整理的SpringBoot入门到成神
    爬虫项目(七):CSDN博客全部文章信息爬取
    SPARKSQL3.0-Antlr4由浅入深&SparkSQL语法解析
    golang 多环境配置切换
    Jenkins Maven pom jar打包未拉取最新包解决办法,亲测可行
  • 原文地址:https://blog.csdn.net/qq_29698805/article/details/133822567