Java 20

De nieuwste upgrade van Java, is inmiddels 2 weken beschikbaar in een production release, geeft een voorproefje van een aantal nieuwe mogelijkheden, waaronder virtual threads en structured concurrency.

Java Development Kit (JDK) 20 is now available as a production release from Oracle. The latest upgrade of standard Java, JDK 20 features incubation or preview versions of seven new capabilities including virtual threads and structured concurrency.

The seven features officially marked for the release, all of which are either in an incubation or in a preview stage, also include a vector API, scoped values, record patterns, pattern matching for switch statements and expressions, and a foreign function and memory API.

These are the specifics on the seven new features in JDK 20:

  • The vector API proposal, not officially added until just a few weeks ago, would introduce an API to express vector computations that reliably compile at runtime as optimal vector instructions on supported CPU architectures. This would achieve performance superior to equivalent scalar computations. The vector API previously has been incubated in JDK 16JDK 17JDK 18, and JDK 19.
  • Virtual threads, in a second preview, are lightweight threads that reduce the effort of writing, maintaining, and observing high-throughput concurrent applications. This capability will fundamentally change how Java applications are scaled, according to Oracle. Minor changes since the first preview in JDK 19 include a small number of API changes (made permanent in JDK 19) and degradations to ThreadGroup (also made permanent in JDK 19).
  • Structured concurrency features an API to simplify multithreaded programming and treats multiple tasks running in different threads as a single unit of work. Error-handling and cancellation are streamlined, improving reliability and enhancing observability. The only change since being incubated in JDK 19 is that StructuredTaskScope has been updated to support inheritance of scoped values by threads created in a task scope. This feature is being reincubated.
  • Scoped values, an API in an incubator stage of development, enables the sharing of immutable data within and across threads. These are preferred to thread-local variables, particularly when using large numbers of virtual threads. A scoped value allows data to be shared safely and efficiently between components in a large program without resorting to method arguments. Goals include ease of use, comprehensibility, robustness, and performance.
  • Record patterns, in a second preview, enhance the Java programming language with patterns to deconstruct record values. Record patterns and type patterns can be nested to enable a declarative, powerful, and composable form of data navigation and processing. Goals include extending pattern matching to express more sophisticated, composable data queries and not changing the syntax or semantics of type patterns. Main changes since the first preview in JDK 19 include adding support for inference of type arguments of generic record patterns, support for record patterns to appear in the header of an enhanced for statement, and removing support for named record patterns.
  • Foreign function and memory API introduces an API by which Java programs can interoperate with code and data outside of the Java runtime. The API enables Java programs to call native libraries and process native data without the brittleness and danger of JNI (Java Native Interface). The API was previously incubated in JDK Versions 17 and 18 and previewed in JDK 19. Refinements added since JDK 19 include unification of MemorySegment and MemoryAddress abstractions, an enhanced MemoryLayout hierarchy, and splitting of MemorySession into Arena and SegmentScope to promote sharing of segments across maintenance boundaries.
  • Pattern matching for switch statements and expressions enables the concise and safe expression of complex data-oriented queries. Previously previewed in JDK 17JDK 18, and JDK 19, this fourth preview would enable a continued co-evolution with Record Patterns, allowing for continued refinements based on experience and feedback. The main changes in pattern matching for switch since the third preview include simplified grammar for switch labels and support for inference of type arguments for generic patterns and record patterns in switch statements and expressions. Also, an exhaustive switch over an enum class now throws MatchException rather than IncompatibleClassChangeError if no switch label applies at runtime.

Oracle has seen a trend where user sites run an LTS version in production while also running the latest six-month release to try it out and give feedback.

The features in JDK 20 are moving toward becoming standardized, Oracle said. These features are derived from Java research projects including Amber, for productivity-oriented capabilities (record patterns and pattern matching for switch); Panama, for interconnecting the JVM with native code (foreign function and memory API and vector API);  which incubates the new JVM features and the APIs built on top of them (scoped values, virtual threads, and structured concurrency).