五种io模型对比以及使用场景,提供java demo

11 篇文章 0 订阅
5 篇文章 0 订阅

标题:对比各种 I/O 方式及其使用场景

在计算机编程中,I/O(输入/输出)操作是非常常见的,它涉及到程序与外部设备(如磁盘、网络、键盘、显示器等)之间的数据交换。随着计算机技术的发展,出现了多种 I/O 方式,每种方式都有其特点、优势和劣势。本文将对比目前为止的各种 I/O 方式,包括阻塞 I/O、非阻塞 I/O、I/O 多路复用、信号驱动 I/O、异步 I/O 等,并给出它们的使用场景和一个 Java 的使用例子。

对比各种 I/O 方式

下表对比了各种 I/O 方式的特点、优势、劣势和使用场景:

I/O 方式特点优势劣势使用场景
阻塞 I/O调用会一直阻塞直到有数据返回简单易用、适合顺序读写性能较差、无法满足并发需求适用于顺序读写、数据量小的情况
非阻塞 I/O调用不会阻塞,但需要轮询检查状态灵活,可以处理多个连接需要不断轮询状态,性能较差适用于需要处理多个连接的情况
I/O 多路复用通过 select/poll/epoll 等系统调用可以同时处理多个连接、高效需要对文件描述符进行复杂的管理、编程复杂适用于需要高并发处理的情况
信号驱动 I/O通过信号通知 I/O 完成简化了状态轮询、适合处理信号驱动的 I/O对信号处理要求高、编程复杂适用于需要对 I/O 事件进行信号驱动处理的情况
异步 I/O调用会立即返回,通过回调函数通知完成高效、适合大规模并发、适合处理大文件编程复杂、不适合处理小数据量、不适合顺序读写适用于需要高效处理大规模并发、大文件的情况

例子(java)

BIO

当使用阻塞式 I/O(BIO)时,通常会使用 SocketServerSocket 来进行网络通信。以下是一个简单的 Java
示例,其中包含一个使用 BIO 的客户端和服务器端。

服务端代码:
import java.io.*;
import java.net.*;

public class SimpleServer {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8080);
            System.out.println("Server started, waiting for client...");

            Socket clientSocket = serverSocket.accept();
            System.out.println("Client connected: " + clientSocket);

            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println("Received from client: " + inputLine);
                out.println("Server received: " + inputLine);
            }

            clientSocket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
客户端代码:
import java.io.*;
import java.net.*;

public class SimpleClient {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 8080);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            out.println("Hello, server!");
            System.out.println("Server response: " + in.readLine());

            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这个例子中,服务端通过 ServerSocket 监听端口,当有客户端连接时,通过 accept() 方法接受连接。客户端通过
Socket 连接到服务端,并发送消息。服务端接收到消息后,发送回应,并关闭连接。这是一个简单的使用阻塞式 I/O
的客户端-服务器端通信例子。

NIO
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class FileCopyExample {
    public static void main(String[] args) {
        try {
            AsynchronousFileChannel sourceChannel = AsynchronousFileChannel.open(Paths.get("source.txt"), StandardOpenOption.READ);
            AsynchronousFileChannel destinationChannel = AsynchronousFileChannel.open(Paths.get("destination.txt"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);

            ByteBuffer buffer = ByteBuffer.allocate(1024);

            sourceChannel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
                @Override
                public void completed(Integer result, ByteBuffer attachment) {
                    attachment.flip();
                    destinationChannel.write(attachment, 0, attachment, new CompletionHandler<Integer, ByteBuffer>() {
                        @Override
                        public void completed(Integer result, ByteBuffer buffer) {
                            if (buffer.hasRemaining()) {
                                destinationChannel.write(buffer, 0, buffer, this);
                            } else {
                                System.out.println("File copy completed");
                            }
                        }

                        @Override
                        public void failed(Throwable exc, ByteBuffer attachment) {
                            System.out.println("File copy failed: " + exc.getMessage());
                        }
                    });
                }

                @Override
                public void failed(Throwable exc, ByteBuffer attachment) {
                    System.out.println("File read failed: " + exc.getMessage());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在上述示例中,我们使用了 Java 的异步 I/O(NIO)API 来实现文件的异步复制操作。通过异步
I/O,可以高效地处理大文件复制操作,而不会阻塞主线程。

AIO

在 Java 中,可以使用 AsynchronousServerSocketChannel
AsynchronousSocketChannel 类来实现基于 AIO(Asynchronous
I/O)的网络通信。以下是一个简单的使用 AIO 的服务器端和客户端的示例代码。

服务端代码:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;

public class SimpleAioServer {
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(8080));
        System.out.println("Server started, waiting for client...");

        server.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
            @Override
            public void completed(AsynchronousSocketChannel client, Void attachment) {
                server.accept(null, this); // 接受下一个连接

                handleClient(client);
            }

            @Override
            public void failed(Throwable exc, Void attachment) {
                // 处理错误
            }
        });

        // 保持服务器运行
        Thread.sleep(10000);
    }

    private static void handleClient(AsynchronousSocketChannel client) {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        client.read(buffer, null, new CompletionHandler<Integer, Void>() {
            @Override
            public void completed(Integer result, Void attachment) {
                if (result == -1) {
                    try {
                        client.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return;
                }

                buffer.flip();
                byte[] data = new byte[buffer.limit()];
                buffer.get(data);
                System.out.println("Received from client: " + new String(data));

                buffer.flip();
                client.write(buffer, null, new CompletionHandler<Integer, Void>() {
                    @Override
                    public void completed(Integer result, Void attachment) {
                        if (buffer.hasRemaining()) {
                            client.write(buffer, null, this);
                        } else {
                            buffer.clear();
                            client.read(buffer, null, this);
                        }
                    }

                    @Override
                    public void failed(Throwable exc, Void attachment) {
                        // 处理错误
                    }
                });
            }

            @Override
            public void failed(Throwable exc, Void attachment) {
                // 处理错误
            }
        });
    }
}
客户端代码:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;

public class SimpleAioClient {
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
        client.connect(new InetSocketAddress("localhost", 8080), null, new CompletionHandler<Void, Void>() {
            @Override
            public void completed(Void result, Void attachment) {
                try {
                    client.write(ByteBuffer.wrap("Hello, server!".getBytes())).get();
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    client.read(buffer, null, new CompletionHandler<Integer, Void>() {
                        @Override
                        public void completed(Integer result, Void attachment) {
                            buffer.flip();
                            byte[] data = new byte[buffer.limit()];
                            buffer.get(data);
                            System.out.println("Server response: " + new String(data));
                            try {
                                client.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void failed(Throwable exc, Void attachment) {
                            // 处理错误
                        }
                    });
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void failed(Throwable exc, Void attachment) {
                // 处理错误
            }
        });

        // 保持客户端运行
        Thread.sleep(5000);
    }
}

在这个例子中,服务端和客户端都使用了 AIO 的方式来进行异步的网络通信。服务端通过
AsynchronousServerSocketChannel 接受连接,并在连接建立后处理客户端的请求。客户端通过
AsynchronousSocketChannel 连接到服务端,并发送消息,然后接收服务端的响应。

开源项目

以下是一些常见的开源组件和框架,它们使用了不同的 I/O 模型:

  1. Nginx:使用事件驱动的 I/O 多路复用模型,通过 epoll 或 kqueue 实现高并发的网络通信。
  2. Node.js:使用非阻塞 I/O 模型和事件驱动架构,基于 JavaScript 运行时环境实现高性能的 I/O 操作。
  3. Netty:一个基于 Java NIO 的异步事件驱动的网络应用框架,适用于快速开发高性能的网络通信程序。
  4. Apache MINA:一个基于 Java NIO 的网络应用框架,提供了高性能的网络通信解决方案。
  5. libuv:一个跨平台的异步 I/O 库,最初由 Node.js 使用,后来也被其他项目采用。
  6. Redis:使用多路复用模型和非阻塞 I/O,实现高性能的内存数据库服务。
  7. Kafka:使用了多路复用模型和异步 I/O,为分布式流处理平台提供了高吞吐量的消息传递功能。

结论

不同的 I/O 方式各有其特点和适用场景。在实际应用中,需要根据具体的需求和系统设计来选择合适的 I/O 方式。希望本文的对比能够帮助读者更好地理解各种 I/O 方式及其使用场景。

  • 37
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值