Java NIO Overview and Example
Java NIO Purpose Netty is a library that abstracts Java NIO, so Java NIO is often discussed. This post provides a brief explanation. Features NonBlocking I/O I/O operations can be performed asynchronously without waiting Multiple connections can be implemented in a non-blocking way (Selectors) Channels Bidirectional communication Unlike InputStream and OutputStream, a single channel can perform both read and write Direct Buffer support Performance improvement by reducing buffer copy overhead through memory-mapped buffers Read/write for socket communication is only supported with native memory - stack overflow (Java Champion’s answer) Native memory is part of the JVM’s native area If you use a buffer created in JVM heap memory for socket communication, there is overhead from copying to native memory To reduce buffer copy overhead, direct buffers are introduced, which are created and written directly in native memory Due to direct buffer optimization, there is a difference in the number of direct buffers between Webflux and MVC models - https://oss.navercorp.com/nspa/nspa-server/issues/3219#issuecomment-14853076 MVC: 200~300 Webflux: 75 Main Components Selector Handles events for multiple channels in a non-blocking way ServerSocketChannel Server socket channel. Supports bidirectional communication. By implementing SelectableChannel, Selector can register to receive events for the channel. SelectionKey Token issued when registering a channel with a Selector. Represents the connection between selector and channel. ...