首页 > 其他分享 >NIO、BIO、AIO区别

NIO、BIO、AIO区别

时间:2024-08-01 10:29:53浏览次数:11  
标签:BIO NIO 异步 buffer void fileChannel AIO ByteBuffer public

BIO 全称 Block-IO 是一种同步且阻塞的通信模式。是一个比较传统的通信方式,模式简单,使用方便。但并发处理能力低,通信耗时,依赖网速。

Java NIO,全程 Non-Block IO ,是 Java SE 1.4 版以后,针对网络传输效能优化的新功能。是一种非阻塞同步的通信模式。

NIO 与原来的 I/O 有同样的作用和目的, 他们之间最重要的区别是数据打包和传输的方式。原来的 I/O 以流的方式处理数据,而 NIO 以块的方式处理数据。

面向流的 I/O 系统一次一个字节地处理数据。一个输入流产生一个字节的数据,一个输出流消费一个字节的数据。

面向块的 I/O 系统以块的形式处理数据。每一个操作都在一步中产生或者消费一个数据块。按块处理数据比按(流式的)字节处理数据要快得多。但是面向块的 I/O 缺少一些面向流的 I/O 所具有的优雅性和简单性。

Java AIO,全称 Asynchronous IO,是异步非阻塞的 IO。是一种非阻塞异步的通信模式。

在 NIO 的基础上引入了新的异步通道的概念,并提供了异步文件通道和异步套接字通道的实现。

区别


BIO (Blocking I/O):同步阻塞 I/O 模式。

NIO (New I/O):同步非阻塞模式。

AIO (Asynchronous I/O):异步非阻塞 I/O 模型。

适用场景

BIO 方式适用于连接数目比较小且固定的架构,这种方式对服务器资源要求比较高,并发局限于应用中,JDK1.4 以前的唯一选择,但程序直观简单易理解。

NIO 方式适用于连接数目多且连接比较短(轻操作)的架构,比如聊天服务器,并发局限于应用中,编程比较复杂,JDK1.4 开始支持。

AIO 方式适用于连接数目多且连接比较长(重操作)的架构,比如相册服务器,充分调用 OS 参与并发操作,编程比较复杂,JDK7 开始支持。

使用方式

public class BioFileDemo {
    public static void main(String[] args) {
        BioFileDemo demo = new BioFileDemo();
        demo.writeFile();
        demo.readFile();
    }

    // 使用 BIO 写入文件
    public void writeFile() {
        String filename = "xx.txt";
        try {
            FileWriter fileWriter = new FileWriter(filename);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            bufferedWriter.write("学编程就上技术派");
            bufferedWriter.newLine();

            System.out.println("写入完成");
            bufferedWriter.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 使用 BIO 读取文件
    public void readFile() {
        String filename = "xx.txt";
        try {
            FileReader fileReader = new FileReader(filename);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println("读取的内容: " + line);
            }

            bufferedReader.close();
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class NioFileDemo {
    public static void main(String[] args) {
        NioFileDemo demo = new NioFileDemo();
        demo.writeFile();
        demo.readFile();
    }

    // 使用 NIO 写入文件
    public void writeFile() {
        Path path = Paths.get("cc.txt");
        try {
            FileChannel fileChannel = FileChannel.open(path, EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE));

            ByteBuffer buffer = StandardCharsets.UTF_8.encode("hh");
            fileChannel.write(buffer);

            System.out.println("写入完成");
            fileChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 使用 NIO 读取文件
    public void readFile() {
        Path path = Paths.get("xx.txt");
        try {
            FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ);
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            int bytesRead = fileChannel.read(buffer);
            while (bytesRead != -1) {
                buffer.flip();
                System.out.println("读取的内容: " + StandardCharsets.UTF_8.decode(buffer));
                buffer.clear();
                bytesRead = fileChannel.read(buffer);
            }

            fileChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class AioDemo {

    public static void main(String[] args) {
        AioDemo demo = new AioDemo();
        demo.writeFile();
        demo.readFile();
    }

    // 使用 AsynchronousFileChannel 写入文件
    public void writeFile() {
        // 使用 Paths.get() 获取文件路径
        Path path = Paths.get("xx.txt");
        try {
            // 用 AsynchronousFileChannel.open() 打开文件通道,指定写入和创建文件的选项。
            AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);

            // 将要写入的字符串("学编程就上技术派")转换为 ByteBuffer。
            ByteBuffer buffer = StandardCharsets.UTF_8.encode("学编程就上技术派");
            // 调用 fileChannel.write() 方法将 ByteBuffer 中的内容写入文件。这是一个异步操作,因此需要使用 Future 对象等待写入操作完成。
            Future<Integer> result = fileChannel.write(buffer, 0);
            // 等待写操作完成
            result.get();

            System.out.println("写入完成");
            fileChannel.close();
        } catch (IOException | InterruptedException | java.util.concurrent.ExecutionException e) {
            e.printStackTrace();
        }
    }

    // 使用 AsynchronousFileChannel 读取文件
    public void readFile() {
        Path path = Paths.get("xx.txt");
        try {
            // 指定读取文件的选项。
            AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
            // 创建一个 ByteBuffer,用于存储从文件中读取的数据。
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            // 调用 fileChannel.read() 方法从文件中异步读取数据。该方法接受一个 CompletionHandler 对象,用于处理异步操作完成后的回调。
            fileChannel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
                @Override
                public void completed(Integer result, ByteBuffer attachment) {
                    // 在 CompletionHandler 的 completed() 方法中,翻转 ByteBuffer(attachment.flip()),然后使用 Charset.forName("UTF-8").decode() 将其解码为字符串并打印。最后,清空缓冲区并关闭文件通道。
                    attachment.flip();
                    System.out.println("读取的内容: " + StandardCharsets.UTF_8.decode(attachment));
                    attachment.clear();
                    try {
                        fileChannel.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void failed(Throwable exc, ByteBuffer attachment) {
                    // 如果异步读取操作失败,CompletionHandler 的 failed() 方法将被调用,打印错误信息。
                    System.out.println("读取失败");
                    exc.printStackTrace();
                }
            });

            // 等待异步操作完成
            Thread.sleep(1000);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

标签:BIO,NIO,异步,buffer,void,fileChannel,AIO,ByteBuffer,public
From: https://www.cnblogs.com/sprinining/p/18336112

相关文章

  • Java I/O的进化:从I/O到NIO
    引言JavaI/O(Input/Output)是Java程序中处理输入和输出的基本方式,包括文件操作、网络通信等。随着Java平台的发展,JavaNIO(NewInput/Output)作为JavaI/O的一个重要补充,提供了更为高效和灵活的I/O操作方式。本文将深入讲解JavaI/O模型、NIO的原理,以及如何在实际应用中使用N......
  • 「NOIP2017_Junior」图书管理员
    题目题目描述图书馆中每本书都有一个图书编码,可以用于快速检索图书,这个图书编码是一个正整数。每位借书的读者手中有一个需求码,这个需求码也是一个正整数。如果一本书的图书编码恰好以读者的需求码结尾,那么这本书就是这位读者所需要的。小D刚刚当上图书馆的管理员,她......
  • 类型提示和@singledispatch:如何以可扩展的方式包含 `Union[...]`?
    我正在重构一个函数,该函数将各种日期格式(即ISO8601字符串、datetime.date、datetime.datetime等)转换为Unix时间戳。我希望新函数使用@singledispatch而不是类型检查,但我不知道如何保留以前函数的类型提示:旧函数:使用类型检查importdat......
  • 如何在 aiogram 中注册多个消息处理程序
    我正在尝试创建多个不同的消息处理程序,这是注册多个消息处理程序的可接受方式吗?:importasynciofromaiogramimportBot,Dispatcher,typesfromsettingsimportBOT_TOKENasyncdefstart_handler(event:types.Message):awaitevent.answer(f"......
  • nohup ./minio server --address :9000 --console-address :9001
    [root@ecm-8cc1minio]#./minioserver/opt/minioINFO:Formatting1stpool,1set(s),1drivesperset.INFO:WARNING:Hostlocalhasmorethan0drivesofset.Ahostfailurewillresultindatabecomingunavailable.MinIOObjectStorageServerCopyright:20......
  • 为什么参数类型“Dict[str, Union[str, int]]”不接受类型“Dict[str, str]”的值(mypy
    我有一个传递给模板的变量字典的类型:VariablesDict=Dict[str,Union[int,float,str,None]]基本上,任何字典,其中键是字符串,值是字符串、数字或None。我在几个与模板相关的函数中使用这种类型。以这个示例函数为例:defrender_template(name:str,variables:Vari......
  • python BioChemist 数据集的数据字典/描述
    我正在使用生物化学家数据集。我在哪里可以找到包含每个变量描述的“数据字典”?这就是我正在查看的:importpandasaspdfrompydatasetimportdatadata('bioChemists')我已经用谷歌搜索并尝试寻找运算符,但没有运气!pydataset软件包不包含生物化学家数据集的描述......
  • Windows安装MinIO
    官方文档1.安装MinIO服务器下载MinIO可执行文件:https://dl.min.io/server/minio/release/windows-amd64/minio.exe下一步包括运行可执行文件的说明。不能从资源管理器或双击文件来运行可执行文件https://www.minio.org.cn/download.shtml#/windows2.启动minioserver......
  • NIO三大组件关系
    JavaNIO同步非阻塞模式,通过Selector选择器使一个线程从某通道(Channel已就绪准备好数据载体),让线程不会因为阻塞没事可做通俗理解:NIO是可以做到用一个线程来处理多个操作的.假设有10000个请求过来,根据实际情况,可以分配50或者100个线程来处理.不像之前的阻塞IO那样,非......
  • 如何使用aioprometheus和FastAPI向外部服务添加指标?
    我正在尝试在使用aioprometheus构建的应用程序中使用FastAPI向外部服务添加指标。这是我想要实现的目标的简化示例。假设我有一个这样的包装器App类:fromaioprometheusimportRegistry,Counter,HistogramfromfastapiimportFastAPIclassApp:......