JDK21来了,有哪些重要更新说明?

摘要:JDK21 计划23年9月19日正式发布,虽然一直以来都是“版本随便出,换 8 算我输”,但这么多年这么多版本的折腾,如果说之前的 LTS版本JDK17你还觉得不香,那 JDK21还是有必要关注一下,因为会有一批重要更新发布到生产环境中,特
JDK21 计划23年9月19日正式发布,虽然一直以来都是“版本随便出,换 8 算我输”,但这么多年这么多版本的折腾,如果说之前的 LTS版本JDK17你还觉得不香,那 JDK21还是有必要关注一下,因为会有一批重要更新发布到生产环境中,特别是千呼万唤的虚拟线程,虽然说这东西我感觉不需要的用不到,需要的早都转go了,哈哈,但作为近几年JDK一个“重大”的更新,在实际开发应用中还是是有很大价值的。所以这篇文章主要提取了这次更新中个人感觉比较有价值的几点做个基本的介绍,想要尝鲜的同学可以看下。 Visual Threads (虚拟线程) -JEP 444 先看下官方对虚拟线程(Visual Threads)描述: Today, every instance ofjava.lang.Threadin the JDK is aplatform thread. A platform thread runs Java code on an underlying OS thread and captures the OS thread for the code's entire lifetime. The number of platform threads is limited to the number of OS threads. Avirtual threadis an instance ofjava.lang.Threadthat runs Java code on an underlying OS thread but does not capture the OS thread for the code's entire lifetime. This means that many virtual threads can run their Java code on the same OS thread, effectively sharing it. While a platform thread monopolizes a precious OS thread, a virtual thread does not. The number of virtual threads can be much larger than the number of OS threads. Virtual threads are a lightweight implementation of threads that is provided by the JDK rather than the OS. They are a form ofuser-mode threads, which have been successful in other multithreaded languages (e.g., goroutines in Go and processes in Erlang). User-mode threads even featured as so-called"green threads"in early versions of Java, when OS threads were not yet mature and widespread. However, Java's green threads all shared one OS thread (M:1 scheduling) and were eventually outperformed by platform threads, implemented as wrappers for OS threads (1:1 scheduling). Virtual threads employ M:N scheduling, where a large number (M) of virtual threads is scheduled to run on a smaller number (N) of OS threads. 总结下就是之前java中的线程是“platform thread”即平台线程,它由操作系统线程为基础,按照1:1的模式调度,这导致线程的创建与执行都是很耗资源的,同时数量也受系统的约束;但新的虚拟线程则是由JDK提供,你可以把它看作是在平台线程基础上创建的“一批”线程,它们有效地共享所属的平台线程也就是操作系统线程的资源,从而提升系统利用率,并不受数量限制。
阅读全文