Java I / O类和性能 [英] Java I/O classes and performance

查看:118
本文介绍了Java I / O类和性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读Java思维第4版。我对I / O操作性能有一些疑问:
我读过最好在BufferedInputStream中包装InputStream对象,但在我看来,我看不出有任何区别。是不是已经缓冲了文件操作?文件缓冲写的优点是什么?

Reading Thinking in Java 4th ed. I've got some doubts about I/O operations performance: I've read that it's better to "wrap" InputStream objects in BufferedInputStream, but in my mind I can't see any difference. Isn't i.e. file operations already buffered? What's the advantages of file buffered write?

推荐答案

系统的IO缓冲与Buffered * putStream不同。

The system's IO buffering is on a different level than the Buffered*putStream.

每次调用 FileOutputStream.write(...)都会引发本机方法调用(通常比java-internal调用),然后上下文切换到OS的内核来进行实际编写。即使内核(或文件系统驱动程序或硬盘控制器或硬盘本身)正在进行更多缓冲,也会发生这些成本。

Each call on FileOutputStream.write(...) induces a native method call (which is typically more costly than a java-internal call), and then a context switch to the OS' kernel to do the actual writing. Even if the kernel (or the file system driver or the harddisk controller or the harddisk itself) is doing more buffering, these costs will occur.

通过在此周围包装BufferedOutputStream ,我们只会更少地调用本机写入方法,从而允许更高的吞吐量。

By wrapping a BufferedOutputStream around this, we will call the native write method only much less often, thus allowing much higher throughput.

(同样适用于其他类型的IO,当然,我只是以FileOutputStream为例。)

(The same is valid for other types of IO, of course, I just used FileOutputStream as an example.)

这篇关于Java I / O类和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆