How much do you know about the latest features of Java 13?

Keywords: Java JDK socket jvm

JDK 13 was officially released on September 17, 2019.

The new version contains five main features

  • JEP 350: Dynamic CDS Archives
  • JEP 351: ZGC: Uncommit Unused Memory
  • JEP 353: Reimplement the Legacy Socket API
  • JEP 354: Switch Expressions (Preview)
  • JEP 355: Text Blocks (Preview)

Dynamic CDS Archives

First, we introduce CDS, which is called Class Data Sharing. The purpose of CDS is to reduce the startup time of Java programming language applications, especially smaller applications, and to reduce memory usage. The idea is to map shared archive files into memory during JVM calls, thus saving the cost of loading these classes and allowing multiple JVM processes to share most of the JVM metadata of these classes.

History of CDS in JVM

  • JDK 1.5 introduces the concept of CDS, which is dumped into a shared archive by converting the core classes in rt.jar into internal representations in advance. Each JVM only needs to load its own application classes, which reduces the startup time. In addition, the core classes are shared, so the memory usage of JVM is reduced.
  • In Oracle JDK 9, this becomes very useful when supporting other GC algorithms and application classes.
  • Before JDK 10, it had always been a commercial feature and became open source in JDK 10.
  • Dynamic CDS archiving is implemented in OpenJDK 13/JDK 13 to improve the availability of application class data sharing (AppCDS). Eliminates the need for users to create a list of classes for each application for trial run. When a program exists, you can use - XX: ArchiveClasses AtExit=<filename> instead of providing a list of classes. Then use - XX: Shared ArchiveFile = < filename > to share class data over the default archive of the JDK system

ZGC: Uncommit Unused Memory

ZGC was introduced in JDK 11, but so far it has not returned unused heap memory to the operating system like the G1 garbage collector. This JEP solves this problem and is enabled by default.

Reimplement the Legacy Socket API

The implementation of java.net.Socket and java.net.ServerSocket is very old, and this JEP introduces new implementations for them. The new implementations are the default implementations in Java 13, but the old implementations have not been deleted yet. They can be used by setting the system property jdk.net.usePlainSocketImpl. But no new implementation has been introduced for java.net.DatagramSocket.

Running a class that instantiates Socket and ServerSocket displays the debug output. This is the default (new implementation):

java -XX:+TraceClassLoading JEP353  | grep Socket
[0.033s][info   ][class,load] java.net.Socket source: jrt:/java.base
[0.035s][info   ][class,load] java.net.SocketOptions source: jrt:/java.base
[0.035s][info   ][class,load] java.net.SocketImpl source: jrt:/java.base
[0.039s][info   ][class,load] java.net.SocketImpl$$Lambda$1/0x0000000800b50840 source: java.net.SocketImpl
[0.042s][info   ][class,load] sun.net.PlatformSocketImpl source: jrt:/java.base
[0.042s][info   ][class,load] sun.nio.ch.NioSocketImpl source: jrt:/java.base
[0.043s][info   ][class,load] sun.nio.ch.SocketDispatcher source: jrt:/java.base
[0.044s][info   ][class,load] java.net.DelegatingSocketImpl source: jrt:/java.base
[0.044s][info   ][class,load] java.net.SocksSocketImpl source: jrt:/java.base
[0.044s][info   ][class,load] java.net.ServerSocket source: jrt:/java.base
[0.045s][info   ][class,load] jdk.internal.access.JavaNetSocketAccess source: jrt:/java.base
[0.045s][info   ][class,load] java.net.ServerSocket$1 source: jrt:/java.base

Nosocket Impl on sun.nio.ch is a new implementation.

Now let's set the system properties and run them again:

$ java -Djdk.net.usePlainSocketImpl -XX:+TraceClassLoading JEP353  | grep Socket
[0.037s][info   ][class,load] java.net.Socket source: jrt:/java.base
[0.039s][info   ][class,load] java.net.SocketOptions source: jrt:/java.base
[0.039s][info   ][class,load] java.net.SocketImpl source: jrt:/java.base
[0.043s][info   ][class,load] java.net.SocketImpl$$Lambda$1/0x0000000800b50840 source: java.net.SocketImpl
[0.046s][info   ][class,load] sun.net.PlatformSocketImpl source: jrt:/java.base
[0.047s][info   ][class,load] java.net.AbstractPlainSocketImpl source: jrt:/java.base
[0.047s][info   ][class,load] java.net.PlainSocketImpl source: jrt:/java.base
[0.047s][info   ][class,load] java.net.AbstractPlainSocketImpl$1 source: jrt:/java.base
[0.047s][info   ][class,load] sun.net.ext.ExtendedSocketOptions source: jrt:/java.base
[0.047s][info   ][class,load] jdk.net.ExtendedSocketOptions source: jrt:/jdk.net
[0.047s][info   ][class,load] java.net.SocketOption source: jrt:/java.base
[0.047s][info   ][class,load] jdk.net.ExtendedSocketOptions$ExtSocketOption source: jrt:/jdk.net
[0.047s][info   ][class,load] jdk.net.SocketFlow source: jrt:/jdk.net
[0.047s][info   ][class,load] jdk.net.ExtendedSocketOptions$PlatformSocketOptions source: jrt:/jdk.net
[0.047s][info   ][class,load] jdk.net.ExtendedSocketOptions$PlatformSocketOptions$1 source: jrt:/jdk.net
[0.048s][info   ][class,load] jdk.net.LinuxSocketOptions source: jrt:/jdk.net
[0.048s][info   ][class,load] jdk.net.LinuxSocketOptions$$Lambda$2/0x0000000800b51040 source: jdk.net.LinuxSocketOptions
[0.049s][info   ][class,load] jdk.net.ExtendedSocketOptions$1 source: jrt:/jdk.net
[0.049s][info   ][class,load] java.net.StandardSocketOptions source: jrt:/java.base
[0.049s][info   ][class,load] java.net.StandardSocketOptions$StdSocketOption source: jrt:/java.base
[0.051s][info   ][class,load] sun.net.ext.ExtendedSocketOptions$$Lambda$3/0x0000000800b51440 source: sun.net.ext.ExtendedSocketOptions
[0.057s][info   ][class,load] java.net.DelegatingSocketImpl source: jrt:/java.base
[0.057s][info   ][class,load] java.net.SocksSocketImpl source: jrt:/java.base
[0.058s][info   ][class,load] java.net.ServerSocket source: jrt:/java.base
[0.058s][info   ][class,load] jdk.internal.access.JavaNetSocketAccess source: jrt:/java.base
[0.058s][info   ][class,load] java.net.ServerSocket$1 source: jrt:/java.base

It shows the old implementation of java.net.PlainSocketImpl that is now in use.

Switch Expressions (Preview)

Switch expression is introduced as a preview feature in JDK 12. JEP 354 modifies this feature by introducing yield statements to return values from blocks instead of using breaks. This means that the switch expression (return value) should use yield, while the switch statement (no return value) should use break.

Switch before JDK 13:

int i;
switch (x) {
    case "1":
        i = 1;
        break;
    case "2":
        i = 2;
        break;
    default:
        i = x.length();
        break;
}

Switch of JDK 13:

switch (x) {
    case "1" -> 1;
    case "2" -> 2;
    default -> {
        int len = x.length();
        yield len;
    }
}

perhaps

switch (x) {
    case "1": yield 1;
    case "2": yield 2;
    default: {
        int len = x.length();
        yield len;
    }
}

Text Blocks (Preview)

The Raw String Literals feature was introduced in JDK 12, but was abandoned before release. JEP is similar in introducing multi-line string text (a text block).

HTML example:

Use string text:

String html = "<html>\n" +
              "    <body>\n" +
              "        <p>Hello, world</p>\n" +
              "    </body>\n" +
              "</html>\n";

Use text blocks:

String html = """
              <html>
                  <body>
                      <p>Hello, world</p>
                  </body>
              </html>
              """;

SQL example

Use string text:

String query = "SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`\n" +
               "WHERE `CITY` = 'INDIANAPOLIS'\n" +
               "ORDER BY `EMP_ID`, `LAST_NAME`;\n";

Use text blocks:

String query = """
               SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`
               WHERE `CITY` = 'INDIANAPOLIS'
               ORDER BY `EMP_ID`, `LAST_NAME`;
               """;

Conclusion:

These are the five new features in JDK 13. Text Blocks and Switch Expressions are the two new features that can change developers'coding style, but they are still in the preview stage.

Moreover, JDK 13 is not LTS (long-term support) version. If you are using Java 8 (LTS) or Java 11 (LTS), you may not need to upgrade to Java 13 for the time being.

To learn more about Java Web technologies, please pay attention to my public number: TomScript To obtain exclusive learning resources and daily dry goods delivery.

Links to the original text: https://blog.sagowiec.com/art...

Posted by Brown on Fri, 11 Oct 2019 01:05:18 -0700