美文网首页
BIO 和 NIO

BIO 和 NIO

作者: _发强 | 来源:发表于2018-08-26 22:27 被阅读0次

概念

BIO:Blocking IO 阻塞线程
NIO:Non-Blocking IO 非阻塞线程

所谓的 BIO , 就是当线程执行了某个耗时操作时,需要等待耗时操作结束,再进行后续操作。
NIO , 就是当线程执行了耗时操作时,该线程不需要等待耗时操作结束,可用来执行其他操作。

例:

这里以文件读写为例,读取一个 文件内容,写入到一个新文件中。
Java 版本

    public static void copyFileBio(File file, File newFile) {
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(file);
            outputStream = new FileOutputStream(newFile);

            byte[] bytes = new byte[1024];
            int byteRead = 0;
            while ((byteRead = inputStream.read(bytes)) > 0) {
                outputStream.write(bytes, 0, byteRead);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(inputStream, outputStream);
        }
    }

    public static void copyFileNio(File file, File newFile) {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(file).getChannel();
            outputChannel = new FileOutputStream(newFile).getChannel();
//            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//            while (true) {
//                byteBuffer.clear();
//                if (inputChannel.read(byteBuffer) < 0) {
//                    break;
//                }
//                outputChannel.write(byteBuffer);
//            }
            // JDK 内部实现,效果等同于 上面所注释部分代码
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(inputChannel, outputChannel);
        }

    }

    public static void close(Closeable... closeables) {
        for (Closeable cloaseable : closeables) {
            if (cloaseable != null) {
                try {
                    cloaseable.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

Kotlin 版

/**
 * BIO 实现
 */
fun copyFileBio(fileOld: File, fileNew: File) {
    FileInputStream(fileOld).use { input ->
        FileOutputStream(fileNew).use { output ->
            val buf = ByteArray(1024)
            while (true) {
                val byteRead = input.read(buf)
                if (byteRead <= 0) {
                    break
                }
                output.write(buf, 0, byteRead)
            }
        }
    }
}

/**
 * NIO 实现
 */
fun copyFileNio(fileOld: File, fileNew: File) {
    FileInputStream(fileOld).channel.use { input ->
        FileOutputStream(fileNew).channel.use { output ->
            //            val byteBuffer = ByteBuffer.allocate(1024)  // NIO 主要是因为这个 ByteBuffer  所以它是一个非阻塞态的一个模型
//            while (true) {
//                byteBuffer.clear()
//                if (input.read(byteBuffer) <= 0) {
//                    break
//                }
//                byteBuffer.flip()   // 移动指针至最新位置
//                output.read(byteBuffer)
//            }
            output.transferFrom(input, 0, input.size()) // JDK 内部实现,效果等同于 上面所注释部分代码
        }
    }
}

相关文章

网友评论

      本文标题:BIO 和 NIO

      本文链接:https://www.haomeiwen.com/subject/dsjaiftx.html