使用缓冲流发送对象? [英] Using buffered streams for sending objects?

查看:88
本文介绍了使用缓冲流发送对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在具有OutputStream而不是BufferedOutputStream的客户端-服务器应用程序中使用Java套接字(输入流也是如此)。

I'm currently using Java sockets in a client-server application with OutputStream and not BufferedOutputStream (and the same for input streams).

客户端和服务器交换序列化的对象(writeObject()方法)。

The client and server exchanges serialized objects (writeObject() method).

在这种情况下使用BufferedOutputStream和BufferedInputStream是否有意义(更快)?

Does it make sense (more speed) to use BufferedOutputStream and BufferedInputStream in this case?

当我必须刷新或不应该编写flush()语句时?

And when I have to flush or should I not write a flush() statement?

推荐答案


在这种情况下使用BufferedOutputStream和BufferedInputStream是否有意义(更快)?

Does it make sense (more speed) to use BufferedOutputStream and BufferedInputStream in this case?

实际上,它可能没有道理 1

对象流实现在内部将已给定的流包装为私有名为执行缓冲的 BlockDataOutputStream 。如果您自己包装流,则将具有两个级别的缓冲...这可能会使性能变差 2

The object stream implementation internally wraps the stream it has been given with a private class called BlockDataOutputStream that does buffering. If you wrap the stream yourself, you will have two levels of buffering ... which is likely to make performance worse2.


当我必须冲洗或不应该编写flush()语句时?

And when I have to flush or should I not write a flush() statement?

是的,冲洗很可能是必要。但是,何时这样做并没有统一的答案。

Yes, flushing is probably necessary. But there is no universal answer as to when to do it.


  • 一方面,如果刷新次数过多,则会产生额外的网络流量。

  • On the one hand, if you flush too often, you generate extra network traffic.

另一方面,如果在需要时不刷新,则服务器可能会停顿等待客户端编写但未刷新的对象。

On the other hand, if you don't flush when it is needed, the server can stall waiting for an object that the client has written but not flushed.

您需要找到这两种综合症之间的折衷方案...并且这取决于您应用程序的客户端/服务器交互模式;例如消息模式是同步的(例如消息/响应)还是异步的(例如消息流)。

You need to find the compromise between these two syndromes ... and that depends on your application's client/server interaction patterns; e.g. whether the message patterns are synchronous (e.g. message/response) or asynchronous (e.g. message streaming).

1-为了确定这一点,您需要做一些法医测试以1)测量系统性能,以及2)确定进行了哪些系统调用以及何时发送网络数据包。对于一般的答案,您需要在多个用例中重复此步骤。我还建议您亲自查看Java库代码以确认我的(简要)阅读。

2-可能只有 a 更糟,但是设计良好的基准测试会带来很小的性能差异。

2 - Probably only a little bit worse, but a well designed benchmark would pick up a small performance difference.

更新

在写完上面的内容之后,我发现了此问题与解答-将Java对象流与套接字一起使用的性能问题-似乎建议使用 BufferedInputStream / BufferedOutputStream 有帮助。但是,由于缓冲的原因,我不确定所报告的性能改善是否是1)真实的(即不是预热伪影)和2)。可能是由于添加了 flush()调用。 (为什么:因为刷新可能导致网络堆栈更快地推送数据。)

After writing the above, I found this Q&A - Performance issue using Javas Object streams with Sockets - which seems to suggest that using BufferedInputStream / BufferedOutputStream helps. However, I'm not certain whether the performance improvement that was reported is 1) real (i.e. not a warmup artefact) and 2) due to the buffering. It could be just due to adding the flush() call. (Why: because the flush could cause the network stack to push the data sooner.)

这篇关于使用缓冲流发送对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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