268. Reimplementing the legacy Socket API
The Socket API has been improved over time and it is still receiving attention for potential further improvements.
Prior to JDK 13, the Socket API (java.net.ServerSocket
and java.net.Socket
) relied on PlainSocketImpl
. Starting with JDK 13 (JEP 353), this API has been replaced by NioSocketImpl
.
As its name suggests, NioSocketImpl
is based on the NIO infrastructure. The new implementation doesn’t rely on the thread stack being capable of taking advantage of buffer cache mechanisms. Moreover, sockets can be closed via the java.lang.ref.Cleaner
mechanism, which gives special attention to how socket objects are garbage collected.
Starting with JDK 15 (JEP 373, follow-on of JEP 353), the internal Socket API has been reimplemented at DatagramSocket
and MulticastSocket
APIs. The goal was to make these APIs simpler and easier to adapt to work with Project Loom (virtual threads).
Whenever you prefer to go for the old PlainSocketImpl...