是否可以非常频繁地调用ObjectOutputStream上的reset()? [英] Is it ok to be calling reset() on an ObjectOutputStream very frequently?

查看:688
本文介绍了是否可以非常频繁地调用ObjectOutputStream上的reset()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到的地方让我不确定并寻找另一种方式。调用 reset()过于频繁地导致网络压力,或者不必要吗?

I read somewhere which has left me unsure and looking for an alternative way. Does calling reset() too frequently cause strain on the network, or unnecessary for this?

我正在发送在ObjectOutputStream上使用TCP的对象。在再次写入之前,对象值会更改。现在是相同的Object但包含不同的值,没有 reset()它重新发送在它之前发送的缓存对象的引用,该引用被读取为没有更改。我不确定使用 reset()是否是一个好主意,因为这种压力。我应该寻找其他方式吗?

I'm sending an object using TCP over an ObjectOutputStream. The objects values get changed before it is written again. Now the same Object but containing different values, without the reset() it resends a reference of the cached object sent before it, which is read to have no changes. I'm not sure if using reset() is a good idea due to such strain. Should I be looking for another way?

示例代码如下:

Socket socket = new Socket(ip, port);

BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(bos);

while(true){
    oos.writeObject(object);
    oos.flush();
    oos.reset();

    object.x++;
}


推荐答案


调用reset()过于频繁会导致网络压力

Does calling reset() too frequently cause strain on the network

与什么相比?如果需要重置,那是因为您需要使用新值传输先前传输的对象。如果您不需要这样做,请不要调用它。如果您确实需要这样做,网络开销与功能要求无关。

Compared to what? If you need to reset, it is because you need to transmit previously transmitted objects with new values. If you don't need to do that, don't call it. If you do need to do it, network overheads are irrelevant to the functional requirement.


或者不必要吗?

or unnecessary for this?

Eh?请翻译?


我在ObjectOutputStream上使用TCP发送对象。对象值在再次写入之前会被更改

I'm sending an object using TCP over an ObjectOutputStream. The objects values get changed before it is written again

因此您需要调用reset()或使用writeUnshared()。

So you need to either call reset() or use writeUnshared().


现在相同的Object但包含不同的值,没有reset()它会重新发送在它之前发送的缓存对象的引用,这将被读取到没有变化。

Now the same Object but containing different values, without the reset() it resends a reference of the cached object sent before it, which is read to have no changes.

正确。所以你不能这样做。

Correct. So you can't do that.


我不确定使用reset()是否是一个好主意,因为这种压力。 / p>

I'm not sure if using reset() is a good idea due to such strain.

什么压力?唯一的压力是你传递物体的新价值,而你的功能要求决定了你必须这样做。所以你没有选择。

What strain? The only 'strain' is that you are transmitting the new value of the object, and your functional requirement dictates that you must do that. So you don't have a choice.


我应该寻找其他方式吗?

Should I be looking for another way?

没有其他办法。要么必须传输对象的新值,要么不传输。如果这样做,所有解决方案都基本相同。如果不这样做,请不要。

There isn't another way. Either you have to transmit the new value of the object or you don't. If you do, all solutions are essentially equivalent. If you don't, don't.

这篇关于是否可以非常频繁地调用ObjectOutputStream上的reset()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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