java 网络 I/O 是否阻塞?

发布于 2024-07-25 23:15:30 字数 326 浏览 6 评论 0原文

希望这是一个关于 java 中的 BufferedOutputStreams 和 DataOutputStreams 的快速而简单的问题。

当我将字节写入输出流时,例如

myBufferedOutputStream.write(array);
myDataOutputStream.write(array);

这些方法是否立即将它们写入流并返回,或者它们是否会阻塞?

我没有在核心java文档中看到任何内容,但也许我的问题没有意义,因为写入永远不会阻塞?

有人请纠正我。

谢谢, 吉布

Hopefully this is a quick and easy question about BufferedOutputStreams and DataOutputStreams in java.

When I write bytes to the output stream like

myBufferedOutputStream.write(array);
myDataOutputStream.write(array);

Do these methods write them instantly to the stream and return or do they ever block?

I did not see anything in the core java docs, but perhaps my question doesn't make sense because writes never block?

Someone please set me straight.

Thanks,
jbu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽手叙旧 2024-08-01 23:15:30

彼得的答案是最好的,但由于一开始就有错误信息,它被破坏了:不,它并不“取决于实施”。 缓冲的全部目的是减轻阻塞的影响,但是当缓冲区已满并且输出硬件未准备好时, write() 将阻塞。 但由于它是缓冲的,因此与普通的 OutputStream 相比,它的阻塞频率要低得多,对吞吐量的影响也要小得多。 缓冲是一种快速、简单地大幅提高性能的方法。

Peter's answer was the best, but it was vitiated by starting out with misinformation: no, it does not "depend on the implementation". The whole point of buffering is to soften the impact of blocking, but when the buffer is full and the output hardware not ready, write() WILL block. But since it is buffered, it will block far less often and with far less impact on throughput than a plain vanilla OutputStream. Buffering is a quick and easy way to make a big improvement in performance.

若有似无的小暗淡 2024-08-01 23:15:30

java.io.* 中的所有读取和写入方法都有可能发生阻塞。 都不支持异步 I/O。 为了阅读,必须使用 .available() 或类似的机制。 对于写作,嗯,你得靠自己。

All of the read and write methods in java.io.* have the potential to block. None support asynchronous I/O. For reading, it must be implemented manually using .available() or a similar mechanism. For writing, well, you're on your own.

維他命╮ 2024-08-01 23:15:30

java.io.* 包中的 API 可能会被阻塞。 然而,有一个称为 Java NIO(新 I/O 或非阻塞 I/O)的特殊 API,您应该将其用于异步 I/O。

查看包java.nio.*
您可以在这里找到一些示例: http://en.wikipedia.org/wiki/New_I/O

The API in the package java.io.* is potential to block. However, there´s a special API called Java NIO (New I/O or Non-blocking I/O) that you should use for aynchronous I/O.

Look at the package java.nio.*
You can find some examples here: http://en.wikipedia.org/wiki/New_I/O

伏妖词 2024-08-01 23:15:30

我相信这取决于实施。 如果您使用 BufferedOutputStream 之类的东西,对 write() 的调用可能不会“阻塞”,因为该类提供了性能缓冲。

但是,如果使用 FileOutputStream,写调用可能会阻塞,具体取决于系统上 I/O 资源的繁忙/可用程度,因为对 write() 的调用实际上可能会阻塞。此时会触发 I/O 操作,该操作需要时间才能完成。

I believe it depends on the implementation. If you're using something like BufferedOutputStream, a call to write() will likely not "block" since the class provides buffering for performance.

However, if using FileOutputStream, the write call may block depending on how busy/available the I/O resources are on the system, since a call to write() may actually trigger an I/O operation at that time, which takes time to complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文