BIO和NIO(特点、使用场景等对比)

BIO和NIO(特点、使用场景等对比)

在 Java 中,BIO 和 NIO 是两种用于处理输入/输出(I/O)的不同编程模型,分别代表 阻塞式 I/O 和 非阻塞式 I/O。下面是它们的详细区别和特点:

1. BIO (Blocking I/O,阻塞式 I/O)

特点

同步、阻塞:

每个 I/O 操作(如读取或写入)都会阻塞当前线程,直到操作完成。适用于连接数较少、I/O 操作简单的场景。线程模型:

每个客户端连接对应一个线程,线程之间没有直接关系。当连接数增加时,系统需要创建更多线程来处理,可能会带来性能瓶颈(线程上下文切换开销大)。使用场景

小型应用或传统网络程序,如简单的文件传输系统。如 Java 的 ServerSocket 和 Socket。import java.io.*;

import java.net.ServerSocket;

import java.net.Socket;

public class BIOServer {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = new ServerSocket(8080);

System.out.println("Server started...");

while (true) {

// 阻塞,直到接收到客户端连接

Socket socket = serverSocket.accept();

System.out.println("Client connected...");

// 创建线程处理客户端请求

new Thread(() -> {

try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) {

String message;

while ((message = reader.readLine()) != null) {

System.out.println("Received: " + message);

writer.println("Echo: " + message);

}

} catch (IOException e) {

e.printStackTrace();

}

}).start();

}

}

}

2. NIO (Non-blocking I/O,非阻塞式 I/O)

特点

同步、非阻塞:

I/O 操作不会阻塞线程,线程可以在等待数据时执行其他任务。使用 Channel 和 Buffer 进行数据操作,而不是传统的 Stream。配合 Selector 实现多路复用(一个线程可以管理多个连接)。高性能:

适用于高并发、大量连接的场景,如聊天室、在线游戏等。减少线程数量,降低系统开销。复杂度较高:

编码较复杂,涉及缓冲区管理和事件驱动模型。使用场景

高并发场景,如高性能服务器、文件传输系统、聊天服务器等。如 Java 的 ServerSocketChannel 和 Selector。

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.Selector;

import java.nio.channels.ServerSocketChannel;

import java.nio.channels.SocketChannel;

import java.nio.channels.SelectionKey;

import java.util.Iterator;

public class NIOServer {

public static void main(String[] args) throws IOException {

// 创建ServerSocketChannel

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

serverSocketChannel.bind(new InetSocketAddress(8080));

serverSocketChannel.configureBlocking(false);

// 创建Selector

Selector selector = Selector.open();

serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

System.out.println("NIO Server started...");

while (true) {

// 监控事件

selector.select();

Iterator keyIterator = selector.selectedKeys().iterator();

while (keyIterator.hasNext()) {

SelectionKey key = keyIterator.next();

keyIterator.remove();

if (key.isAcceptable()) {

// 接收客户端连接

SocketChannel socketChannel = serverSocketChannel.accept();

socketChannel.configureBlocking(false);

socketChannel.register(selector, SelectionKey.OP_READ);

System.out.println("Client connected...");

} else if (key.isReadable()) {

// 读取数据

SocketChannel socketChannel = (SocketChannel) key.channel();

ByteBuffer buffer = ByteBuffer.allocate(1024);

int bytesRead = socketChannel.read(buffer);

if (bytesRead > 0) {

buffer.flip();

System.out.println("Received: " + new String(buffer.array(), 0, buffer.limit()));

// 回写数据

socketChannel.write(ByteBuffer.wrap(("Echo: " + new String(buffer.array(), 0, buffer.limit())).getBytes()));

} else if (bytesRead == -1) {

socketChannel.close();

}

}

}

}

}

}

3. BIO 与 NIO 对比

特性BIO(阻塞 I/O)NIO(非阻塞 I/O)阻塞模式阻塞非阻塞连接处理每个连接一个线程一个线程管理多个连接性能线程数多,性能瓶颈明显更高效,适合高并发复杂性简单易用复杂,需要理解事件驱动和多路复用使用场景小型应用,低并发场景高并发场景,大型分布式系统

相关推荐

网约车驾驶员资格证网上报名申请
日博365官网网址多少

网约车驾驶员资格证网上报名申请

📅 07-18 👁️ 4142
《尘埃3》正式加入Steam联机平台 完全弃坑GFWL
365cc彩票老版

《尘埃3》正式加入Steam联机平台 完全弃坑GFWL

📅 07-13 👁️ 4099
‎烽火终端
365cc彩票老版

‎烽火终端

📅 07-06 👁️ 9219