Java21新特性Demo

一、前言

Java21版本于 2023 年 9 月 19 日发布,自从 Java 改为 6 个月发布一个版本以来,此为如期交付的第 12 个版本。Java21 是 Java 17之后的目前最新的 LTS版本(长期技术支持版本),其中备受关注的新特性是虚拟线程和分带ZGC。虚拟线程在 Java 21中将成为正式功能。Java 21 有望将会成为继 java8 之后又一个流行的 Java 版本。

接下来,让我们一探究竟,和大家分享Java21的新特性。

二、Java21更新内容介绍

官方release公告:https://jdk.java.net/21/release-notes

三、JDK21官方下载地址

OpenJDK 版本:https://jdk.java.net/21/

Oracle 版本:https://www.oracle.com/java/technologies/downloads/

四、新特性汇总

4.1 序列集合

@Slf4j
public class SequencedCollectionDemo {
    public static void main(String[] args) {
        // 创建一个Sequenced Collection
        List<String> sequencedList = new ArrayList<>();
        // 添加元素
        sequencedList.add("A");
        sequencedList.add("B");
        sequencedList.add("C");
        sequencedList.add("D");
        log.info("倒序输出:{}",sequencedList.reversed());
        log.info("JAVA8获取首元素:{}",sequencedList.get(0));
        log.info("JAVA21获取首元素:{}",sequencedList.getFirst());
        log.info("JAVA8获取尾元素:{}",sequencedList.get(sequencedList.size() - 1));
        log.info("JAVA21获取尾元素:{}",sequencedList.getLast());
        // 删除首个元素
        sequencedList.removeFirst();
        // 删除末尾元素
        sequencedList.removeLast();
        log.info("删除首尾元素后:{}",sequencedList);
    }
}

4.2 记录模式

public class RecordTest {

    record Point(int x, int y) {}

    record Size(int width, int height) { }

    record WindowFrame(Point origin, Size size) { }

    static void printSum(Object obj) {
        if (obj instanceof Point p) {
            int x = p.x();
            int y = p.y();
            System.out.println(x+y);
        }
    }

    static void printSumRecord(Object obj) {
        if (obj instanceof Point(int x, int y)) {
            System.out.println(x+y);
        }
    }

    static void printHeight(Object obj) {
        if (obj instanceof WindowFrame wf) {
            if (wf.size() != null) {
                System.out.println("Height: " + wf.size().height());
            }
        }
    }

    static void printHeightRecord(Object obj) {
        if (obj instanceof WindowFrame(Point origin, Size(int width, int height))) {
            System.out.println("Height: " + height);
        }
    }

    public static void main(String[] args) {
        printSum(new Point(1, 2));
        printSumRecord(new Point(1, 2));
        printHeight(new WindowFrame(new Point(1, 2), new Size(3, 4)));
        printHeightRecord(new WindowFrame(new Point(1, 2), new Size(3, 4)));
    }
}

4.3 switch模式匹配

public class SwitchDemo {
    public static void main(String[] args) {
        // 单匹配,不用再写break,支持返回值
        System.out.println(swtichTest01());
        // 执行多行代码,可使用yield返回
        // System.out.println(swtichTest02());
        // 多匹配,直接逗号分割
        // System.out.println(swtichTest03());
        // 匹配增强
        // System.out.println(formatter(1));
        // System.out.println(formatterPatternSwitch(100L));
        // 对于null处理
        // testFooBar(null);

    }

    public static String swtichTest01() {
        String day = "MONDAY";
        return switch (day) {
            case "MONDAY" -> "星期1";
            case "TUESDAY" -> "星期2";
            case "WEDNESDAY" -> "星期3";
            case "THURSDAY" -> "星期4";
            case "FRIDAY" -> "星期5";
            case "SATURDAY" -> "星期6";
            case "SUNDAY" -> "星期7";
            default -> "未知";
        };
    }

    public static String swtichTest02() {
        String day = "MONDAY";
        return switch (day) {
            case "MONDAY" -> {
                System.out.println("周一...");
                yield "星期1";
            }
            case "TUESDAY" -> "星期2";
            case "WEDNESDAY" -> "星期3";
            case "THURSDAY" -> "星期4";
            case "FRIDAY" -> "星期5";
            case "SATURDAY" -> "星期6";
            case "SUNDAY" -> "星期7";
            default -> "未知";
        };
    }

    public static String swtichTest03() {
        String day = "SATURDAY";
        return switch (day) {
            case "MONDAY" -> {
                System.out.println("周一...");
                yield "星期1";
            }
            case "TUESDAY" -> "星期2";
            case "WEDNESDAY" -> "星期3";
            case "THURSDAY" -> "星期4";
            case "FRIDAY" -> "星期5";
            case "SATURDAY","SUNDAY" ->  "周末";
            default -> "未知";
        };
    }

    // java21版本之前
    static String formatter(Object obj) {
        String formatted = "unknown";
        if (obj instanceof Integer i) {
            formatted = String.format("int %d", i);
        } else if (obj instanceof Long l) {
            formatted = String.format("long %d", l);
        } else if (obj instanceof Double d) {
            formatted = String.format("double %f", d);
        } else if (obj instanceof String s) {
            formatted = String.format("String %s", s);
        }
        return formatted;
    }

    //java21
    static String formatterPatternSwitch(Object obj) {
        return switch (obj) {
            case Integer i -> String.format("int %d", i);
            case Long l    -> String.format("long %d", l);
            case Double d  -> String.format("double %f", d);
            case String s  -> String.format("String %s", s);
            default        -> obj.toString();
        };
    }

    static void testFooBar(String s) {
        switch (s) {
            case null         -> System.out.println("Oops");
            case "Foo", "Bar" -> System.out.println("Great");
            default           -> System.out.println("Ok");
        }
    }
}

4.4 虚拟线程

public class VirtualThreadsDemo {


    /**
     * 基于8个线程池实现的虚拟线程
     * 执行10000个任务,每个任务耗时100毫秒,总共耗费3515毫秒
     */
    public static void virtualThread(int count) throws InterruptedException {
        ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
        StopWatcher stopWatcher = new StopWatcher();
        stopWatcher.start();
        CountDownLatch countDownLatch = new CountDownLatch(count);
        for (int i = 0; i < count; i++) {
            executorService.submit(new TestRunner(countDownLatch));
            // CompletableFuture.runAsync(new TestRunner(countDownLatch), Executors.newVirtualThreadPerTaskExecutor());
        }
        countDownLatch.await();
        stopWatcher.stop();
        System.out.printf("本次执行耗时:%s毫秒", stopWatcher.getTimeInterval().toMillis());
    }

    /**
     * 基于7个池化线程
     * 执行一万个任务,每个任务耗时100毫秒 156459毫秒
     */
    public static void platformThread(int count) throws InterruptedException {
        StopWatcher stopWatcher = new StopWatcher();
        stopWatcher.start();
        CountDownLatch countDownLatch = new CountDownLatch(count);
        for (int i = 0; i < count; i++) {
            CompletableFuture.runAsync(new TestRunner(countDownLatch));
        }
        countDownLatch.await();
        stopWatcher.stop();
        System.out.printf("本次执行耗时:%s毫秒", stopWatcher.getTimeInterval().toMillis());
    }

    public static void main(String[] args) throws Exception {
        // 虚拟线程
        // virtualThread(10_000);
        // 平台线程
        platformThread(10_000);

    }

    public static class TestRunner implements Runnable {

        private final CountDownLatch countDownLatch;

        public TestRunner(CountDownLatch countDownLatch) {
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void run() {
            try {
                // System.out.println(Thread.currentThread() + " start " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                Thread.sleep(100);
                // System.out.println(Thread.currentThread() + " stop " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                countDownLatch.countDown();
                // 未命名模式和变量
            } catch (Exception _) {
                System.out.println("An error has occurred!");
            }
        }
    }


    public static class StopWatcher {
        private long start;

        private long stop;

        public StopWatcher() {
        }

        public void start() {
            this.start = System.currentTimeMillis();
        }

        public void stop() {
            this.stop = System.currentTimeMillis();
        }

        public Duration getTimeInterval() {
            return Duration.ofMillis(this.stop - this.start);
        }


    }
}

五、总结

他强由他强,清风拂山冈;

他横由他横,明月照大江;

他发任他发,我用Java8;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值