Performance issue using Javas Object streams with Sockets

蹲街弑〆低调 提交于 2019-11-29 09:43:30

Have you tried embedding both reques ts and responses in BufferedInputStream/BufferedOutputStream ? It should widely improve performances.

So construct the BufferedOutputStream & flush it before constructing the BufferedInputStream. To avoid hanging.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4788782

it says as per documentation

If you modify the test case such that both the AServer and AClient construct the ObjectOutputStream before the ObjectInputStream, the test does not block. This is the expected behaviour given the following documentation:

ObjectOutputStream constructor:
Creates an ObjectOutputStream that writes to the specified
OutputStream. This constructor writes the serialization stream header to the underlying stream; callers may wish to flush the stream immediately to ensure that constructors for receiving
ObjectInputStreams will not block when reading the header.

ObjectInputStream constructor:
Creates an ObjectInputStream that reads from the specified
InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding
ObjectOutputStream has written and flushed the header.

In addition to using Buffered streams and calling flush() before every read, you should also create the ObjectOutputStream first at both ends. Also, testing for readObject() returning null is pointless unless you're planning to call writeObject(null). The test for EOS with readObject() is catch (EOFException exc).

Henrik Gustafsson

I still would have thought it would be faster than that. Is there anything else I can do to improve this?

This seems like something of a micro-benchmark. They're always hard to get right, but I think if you start by sending say 2000 messages before starting your latency measurements.

Also, see this detailed answer on how to do micro-benchmarks right.

I would expect that you have to call objectOutputStream.flush() on both sides to ensure that the data is immediately sent to the network. Otherwise, the TCP stack may wait a while for more data to fill a partial IP packet.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!