Java(JDK)13新特性之Reimplement the Legacy Socket API
使用易于维护和调试的更简单、更现代的实现替换 java.net.Socket 和 java.net.ServerSocket API。
java.net.Socket和java.net.ServerSocket的实现非常古老,这个JEP为它们引入了一个现代的实现。现代实现是Java 13中的默认实现,但是旧的实现还没有删除,可以通过设置系统属性jdk.net.usePlainSocketImpl来使用它们。
运行一个实例化Socket和ServerSocket的类将显示这个调试输出。这是默认的(新的):
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
上面输出的sun.nio.ch.NioSocketImpl就是新提供的实现。
如果使用旧的实现也是可以的(指定参数jdk.net.usePlainSocketImpl):
$ 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
上面的结果中,旧的实现java.net.PlainSocketImpl被用到了。