Discuss / Java / 尊敬的廖大佬,OrderEntity 中的copy()方法,有个verion判断是啥意图啊,保证线程安全?

尊敬的廖大佬,OrderEntity 中的copy()方法,有个verion判断是啥意图啊,保证线程安全?

Topic source

何霖

#1 Created at ... [Delete] [Delete and Lock User]

而且,version判断是在部分属性赋值的时候。

@Nullable

public OrderEntity copy() {

    OrderEntity entity = new OrderEntity();

    int ver = this.version;

    entity.status = this.status;

    entity.unfilledQuantity = this.unfilledQuantity;

    entity.updatedAt = this.updatedAt;

    if (ver != this.version) {

        return null;

    }

    entity.createdAt = this.createdAt;

    entity.direction = this.direction;

    entity.id = this.id;

    entity.price = this.price;

    entity.quantity = this.quantity;

    entity.sequenceId = this.sequenceId;

    entity.userId = this.userId;

    return entity;

}

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

是为了确保复制的时候数据一致性,如果复制了status后status改了,再复制unfilledQuantity他俩就不是一个时刻的了

后面复制的是不变数据,不需要判断一致性


  • 1

Reply