简体   繁体   English

发送网络数据,可变与不可变数据结构

[英]Sending network data, mutable vs immutable data structure

I'm implementing a library for a network protocol that involves sending data at a constant rate of 50Hz. 我正在实现一个网络协议库,它涉及以50Hz的恒定速率发送数据。 The actual transmission of data will be done on a separate thread. 实际的数据传输将在一个单独的线程上完成。 As part of the library, there will be a class that represents all the data that needs to be sent, and the library will take care of serializing it and transmitting it. 作为库的一部分,将有一个表示需要发送的所有数据的类,并且库将负责序列化并传输它。

My issue is that I'm not sure how to design this data class. 我的问题是我不确定如何设计这个数据类。 The options I was thinking of were: 我想到的选项是:

  • Single mutable object. 单个可变对象。 The sending thread will lock the object, serialize it, then unlock it so the client can modify it again. 发送线程将锁定对象,序列化它,然后解锁它,以便客户端可以再次修改它。 I think this is probably the worst option. 我认为这可能是最糟糕的选择。
  • Mutable object, that is cloned by the library when passed as an argument. 可变对象,当作为参数传递时由库克隆。 This way the calling method can modify the existing object while not interfering with the send thread. 这样,调用方法可以修改现有对象,同时不会干扰发送线程。
  • Immutable object, so a new one will have to be created each time by the calling method, and repopulated with the data. 不可变对象,因此每次调用方法都必须创建一个新对象,并重新填充数据。

I was not sure about going the cloning route since a new object will have to be created and populated 50 times per second. 我不确定是否要进行克隆路由,因为必须每秒创建一个新对象并填充50次。

For the cloning options, I was also wondering what the best way to clone an object like this would be. 对于克隆选项,我也想知道克隆像这样的对象的最佳方法是什么。 Should I take advantage of Object.clone(), which I have heard mixed things about, or implement a custom copy method/constructor? 我应该利用Object.clone(),我听说过混合的东西,或者实现自定义复制方法/构造函数? If I make something custom, what would be the most robust way of implementing it? 如果我定制一些东西,那么最强大的实现方法是什么? Basically, I'll have to write in the code to copy every field of the source object one by one, and I was hoping there would be some easier way of doing it. 基本上,我必须在代码中编写一个接一个地复制源对象的每个字段,我希望有一些更简单的方法。

I'd go with a mutable object and cloning. 我会选择一个可变对象和克隆。 Without cloning you have potential data corruption, and making your class immutable adds complexity which is probably unwarranted. 如果没有克隆,你就会有潜在的数据损坏,并且使你的类不可变会增加复杂性,这可能是没有根据的。

Yes, your class should implement Cloneable and override Object.clone() . 是的,您的类应该实现Cloneable并覆盖Object.clone() If any of your instance variable types are mutable, you should clone them too. 如果您的任何实例变量类型是可变的,您也应该克隆它们。 For example: 例如:

class Data implements Cloneable {
    private int i; // int type is immutable
    private String s; // String type is immutable
    private List<String> l; // List type is mutable

    @Override
    public Data clone() {
        try {
            Data clone = (Data) super.clone();
            clone.l = new ArrayList<String>(l); // copy the mutable list
            return clone;
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e); // impossible
        }
    }

    // other class members
}

I would go with the mutable object cloned as it maintains your original object. 我会使用克隆的可变对象,因为它维护您的原始对象。 With this solution you can clone your object to send it and modify the original object for the next sending task. 使用此解决方案,您可以克隆对象以发送它并修改原始对象以进行下一个发送任务。

The cloning mecanism should not be a performance problem as cloning 50 objects per second should not be a problem for any modern system (although I don't know what system are you working on). 克隆机制不应该是一个性能问题,因为克隆每秒50个对象不应该成为任何现代系统的问题(虽然我不知道你在做什么系统)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM