JAVA BIO 与 NIO

 2024-07-25    0 条评论    322 浏览

IO

在Java开发中,BIO(Blocking I/O)和NIO(Non-blocking I/O)是两种用于处理输入输出操作的技术。它们各有特点和适用场景。以下是对两者的详细解释:

BIO(Blocking I/O)

  1. 阻塞模式:BIO采用的是阻塞模式。在进行I/O操作时,如果没有数据可读或没有空间写入,线程将被阻塞,直到有数据可读或有空间可写。这种模式下,每一个连接都需要一个独立的线程来处理。

  2. 实现方式

    • SocketServerSocket:用于网络通信。
    • InputStreamOutputStream:用于处理字节流的输入和输出。
    • ReaderWriter:用于处理字符流的输入和输出。
  3. 优缺点

    • 优点:实现简单,代码直观,适用于连接数较少且连接时间较长的应用。
    • 缺点:线程资源消耗大,在高并发场景下性能较差,因为每一个连接都需要一个独立的线程。
  4. 使用场景:适用于连接数较少且连接时间较长的场景,比如传统的C/S系统、管理后台系统等。

NIO(Non-blocking I/O)

  1. 非阻塞模式:NIO采用的是非阻塞模式。在进行I/O操作时,如果没有数据可读或没有空间写入,线程不会被阻塞,而是继续进行其他操作。NIO允许一个线程管理多个连接。

  2. 实现方式

    • Channels:用于读取和写入数据,比传统的流更加灵活。
    • Buffers:用于存储数据。所有数据在通道中读取或写入都必须通过缓冲区。
    • Selectors:用于管理多个通道,可以注册多个通道到一个选择器上,通过选择器监控这些通道的状态。
  3. 优缺点

    • 优点:能够处理大量连接,线程资源消耗小,适用于高并发场景。
    • 缺点:实现复杂,编程模型相对困难,尤其是处理复杂的状态机时。
  4. 使用场景:适用于连接数多且连接时间短的场景,比如聊天室、WebSocket等高并发应用。

具体代码示例

BIO示例

// BIO 服务器示例
import java.io.*;
import java.net.*;

public class BIOServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        while (true) {
            Socket socket = serverSocket.accept();
            new Thread(() -> {
                try {
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                        System.out.println("Received: " + inputLine);
                        out.println("Echo: " + inputLine);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

NIO示例

// NIO 服务器示例
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 NIOServer {
    public static void main(String[] args) throws IOException {
        Selector selector = Selector.open();
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(8080));
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            selector.select();
            Set<SelectionKey> selectedKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectedKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                if (key.isAcceptable()) {
                    ServerSocketChannel server = (ServerSocketChannel) key.channel();
                    SocketChannel client = server.accept();
                    client.configureBlocking(false);
                    client.register(selector, SelectionKey.OP_READ);
                } else if (key.isReadable()) {
                    SocketChannel client = (SocketChannel) key.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(256);
                    client.read(buffer);
                    buffer.flip();
                    client.write(buffer);
                    buffer.clear();
                }
                iterator.remove();
            }
        }
    }
}

总结

  • BIO适用于小规模连接且连接时间长的场景,编程简单但不适合高并发。
  • NIO适用于高并发和大量连接的场景,编程复杂但资源利用率高。

根据具体需求选择合适的I/O模型,能有效提升应用程序的性能和扩展性。