|
| 1 | +# Java 18 |
| 2 | + |
| 3 | +To run each example use: `java --enable-preview --source 18 <FileName.java>` |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +### Language |
| 8 | + |
| 9 | +* Pattern matching for `switch` |
| 10 | + * Minor improvements from JDK 18: |
| 11 | + * changed guarded pattern from `&&` to `when` keyword |
| 12 | + * definition: guard is the boolean expression, guarded pattern is the case with guard |
| 13 | + * guarded pattern: `case Hero h when h.getCity() == Cities.NEW_YORK` |
| 14 | + * guard: `h.getCity() == Cities.NEW_YORK` |
| 15 | +* Virtual Threads (user-mode threads) |
| 16 | + * `Virtual threads are lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications` |
| 17 | + * definitions: |
| 18 | + * virtual thread is an instance of `java.lang.Thread` that is not tied to a particular OS thread, only consumes an OS thread only while it performs calculations on the CPU; |
| 19 | + * platform thread is an instance of `java.lang.Thread` implemented a thin wrapper around an OS thread. |
| 20 | + * blocking: |
| 21 | + * `when code running in a virtual thread calls a blocking I/O operation in the java.* API, the runtime performs a non-blocking OS call and automatically suspends the virtual thread until it can be resumed later`; |
| 22 | + * `virtual threads preserve the reliable thread-per-request style that is harmonious with the design of the Java Platform while utilizing the hardware optimally` |
| 23 | + * 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. |
| 24 | + * `Virtual threads are not faster threads — they do not run code any faster than platform threads. They exist to provide scale (higher throughput), not speed (lower latency). There can be many more of them than platform threads, so they enable the higher concurrency needed for higher throughput according to Little's Law.` |
| 25 | + * scenarios where it can significantly improve throughput when: |
| 26 | + * number of concurrent tasks is high, and |
| 27 | + * work is not CPU-bound. |
| 28 | + * support thread-local variables and interruption |
| 29 | + * more notes about Project Loom [here](../java-loom/) |
| 30 | + * we can use `Executors.newVirtualThreadPerTaskExecutor` to create virtual threads |
| 31 | + |
| 32 | +## JEPs |
| 33 | + |
| 34 | +* [422](https://openjdk.java.net/jeps/422) - Linux/RISC-V Port |
| 35 | +* [424](https://openjdk.java.net/jeps/424) - Foreign Function & Memory API (Preview) |
| 36 | +* [425](https://openjdk.java.net/jeps/425) - Virtual Threads (Preview) |
| 37 | +* [426](https://openjdk.java.net/jeps/426) - Vector API (Fourth Incubator) |
| 38 | +* [427](https://openjdk.java.net/jeps/427) - Pattern Matching for switch (Third Preview) |
| 39 | + |
| 40 | +## Links |
| 41 | + |
| 42 | +* [JDK 19 Jeps](https://openjdk.java.net/projects/jdk/19/) |
0 commit comments