Java NIO(Non-blocking I/O)是一种基于通道(Channel)和缓冲区(Buffer)的 I/O 模型,支持非阻塞通信和多路复用,适合高并发场景。相比传统的阻塞 I/O(BIO),NIO 更高效,因为它避免了线程被阻塞,降低了系统资源消耗。
代码样例:https://gitee.com/lhdxhl/springboot-example.git
核心组件:
NIO 的非阻塞特性通过 Selector 实现:
NIO 常见的应用场景:
下面以简单的 NIO Socket 服务器 和 客户端 实现为例,展示如何使用 NIO 处理网络通信。
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
public class NioServer {
public static void main(String[] args) throws IOException {
// 1. 创建 ServerSocketChannel
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.bind(new InetSocketAddress(8080));
serverChannel.configureBlocking(false); // 设置为非阻塞模式
// 2. 创建 Selector
Selector selector = Selector.open();
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("NIO Server started on port 8080...");
while (true) {
selector.select(); // 阻塞等待事件发生
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey key = keys.next();
keys.remove();
if (key.isAcceptable()) { // 处理连接事件
handleAccept(key);
} else if (key.isReadable()) { // 处理读事件
handleRead(key);
}
}
}
}
private static void handleAccept(SelectionKey key) throws IOException {
ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
SocketChannel clientChannel = serverChannel.accept();
clientChannel.configureBlocking(false);
System.out.println("Connected to client: " + clientChannel.getRemoteAddress());
clientChannel.register(key.selector(), SelectionKey.OP_READ);
}
private static void handleRead(SelectionKey key) throws IOException {
SocketChannel clientChannel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int bytesRead = clientChannel.read(buffer);
if (bytesRead == -1) {
System.out.println("Client disconnected: " + clientChannel.getRemoteAddress());
clientChannel.close();
key.cancel();
return;
}
buffer.flip();
String message = new String(buffer.array(), 0, buffer.limit());
System.out.println("Received: " + message);
// 回显消息
clientChannel.write(ByteBuffer.wrap(("Echo: " + message).getBytes()));
}
}
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class NioClient {
public static void main(String[] args) {
try {
// 1. 创建 SocketChannel
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
// 2. 连接服务器
if (!socketChannel.connect(new InetSocketAddress("localhost", 8080))) {
while (!socketChannel.finishConnect()) {
System.out.println("Connecting to server...");
}
}
System.out.println("Connected to server!");
// 3. 发送数据
String message = "Hello NIO Server!";
ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());
socketChannel.write(buffer);
// 4. 接收服务器响应
buffer.clear();
socketChannel.read(buffer);
buffer.flip();
System.out.println("Received from server: " + new String(buffer.array(), 0, buffer.limit()));
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务端维护多个客户端连接,并实现消息广播。
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class NioChatServer {
private static final int PORT = 8082;
public static void main(String[] args) throws IOException {
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.bind(new InetSocketAddress(PORT));
serverChannel.configureBlocking(false);
Selector selector = Selector.open();
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("Chat Server started on port " + PORT);
while (true) {
selector.select();
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey key = keys.next();
keys.remove();
if (key.isAcceptable()) {
handleAccept(key, selector);
} else if (key.isReadable()) {
handleRead(key, selector);
}
}
}
}
private static void handleAccept(SelectionKey key, Selector selector) throws IOException {
ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
SocketChannel clientChannel = serverChannel.accept();
clientChannel.configureBlocking(false);
clientChannel.register(selector, SelectionKey.OP_READ);
System.out.println("Client connected: " + clientChannel.getRemoteAddress());
}
private static void handleRead(SelectionKey key, Selector selector) throws IOException {
SocketChannel clientChannel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int bytesRead = clientChannel.read(buffer);
if (bytesRead == -1) {
System.out.println("Client disconnected: " + clientChannel.getRemoteAddress());
clientChannel.close();
key.cancel();
return;
}
buffer.flip();
String message = new String(buffer.array(), 0, buffer.limit());
System.out.println("Received: " + message);
// 广播消息
broadcastMessage(selector, clientChannel, message);
}
private static void broadcastMessage(Selector selector, SocketChannel sender, String message) throws IOException {
Set<SelectionKey> keys = selector.keys();
for (SelectionKey key : keys) {
if (key.channel() instanceof SocketChannel && key.channel() != sender) {
SocketChannel clientChannel = (SocketChannel) key.channel();
clientChannel.write(ByteBuffer.wrap(message.getBytes()));
}
}
}
}
服务器实现定时心跳检测,断开空闲连接。
private static void handleHeartbeat(Selector selector) {
Set<SelectionKey> keys = selector.keys();
for (SelectionKey key : keys) {
if (key.channel() instanceof SocketChannel) {
SocketChannel clientChannel = (SocketChannel) key.channel();
try {
clientChannel.write(ByteBuffer.wrap("HEARTBEAT".getBytes()));
} catch (IOException e) {
try {
clientChannel.close();
key.cancel();
System.out.println("Disconnected idle client.");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
NIO 提供了一种高效的非阻塞 I/O 解决方案,在高并发场景中表现出色。通过实践样例,我们可以快速构建高效的网络通信服务。同时,NIO 的复杂性要求我们在实际开发中关注编码质量与扩展性。