gpt4 book ai didi

java - 为什么对象的状态可以是对象图中以该对象为根的字段的子集?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:04 25 4
gpt4 key购买 nike

我正在阅读“Java concurrency in practice”,起初,它说:

An object's state starts with its fields. If they are all of primitive type, the fields comprise the entire state.

If the object has fields that are references to other objects, its state will encompass fields from the referenced objects as well.

然后它说:

an object's state could be a subset of the fields in the object graph rooted at that object. Why might it be a subset? Under what conditions are fields reachable from a given object not part of that object's state?

我在书中找不到两个问题的答案。

  • 为什么它可能是一个子集?
  • 在什么条件下,从给定对象可访问的字段不属于该对象状态的一部分?

而且我对上面的两个引用完全感到困惑。这对我来说似乎是矛盾的。有人可以举一个例子来说明“对象的状态是对象图中以该对象为根的字段的子集”并回答这两个问题吗?

最佳答案

快速回答

从评论来看,您的困惑似乎是:

I think the meaning is "all fields reachable from am object are part of the object's state". Isn't it?

不,不是这个意思。作者提出的问题表达了这一点:

Under what conditions are fields reachable from a given object not part of that object's state?

很快就得到了回答:

Collection classes often exhibit a form of “split ownership”, in which the collection owns the state of the collection infrastructure, but client code owns the objects stored in the collection


了解所有权

为清楚起见,所有权决定了谁可以为该状态实现同步策略。

In many cases, ownership and encapsulation go together—the object encapsulates the state it owns and owns the state it encapsulates. It is the owner of a given state variable that gets to decide on the locking protocol used to maintain the integrity of that variable’s state

当状态被封装时,客户端被迫通过任何封装状态与状态交互。因此,封装者是独占者,线程安全由独占者决定。

once you publish a reference to a mutable object, you no longer have exclusive control; at best, you might have “shared ownership”.

如果对象被公开(可能通过 getter),封装者将失去独占所有权,因为客户端现在可以绕过封装者设置的任何策略。

但是,这不会改变对象仍然部分由封装器拥有的事实 - 当客户端通过封装器修改对象的状态时,封装器可能仍会强制执行它的策略。


回答...

Why may it be a subset?

摘自本书:

If you allocate and populate a HashMap, you are creating multiple objects: the HashMap object, a number of Map.Entry objects used by the implementation of HashMap, and perhaps other internal objects as well.

HashMap 可能由许多 Map.Entry 对象组成,形式为 HashMap#Node .

由于 HashMap 控制了客户端如何与 Node 交互(客户端可能不会创建新的 Node 实例,只能通过 HashMap#putVal 决定状态如Node#hash),Node的状态被认为是HashMap的子集>.


回答...

Under what conditions are fields reachable from a given object not part of that object's state?

摘自本书:

Collection classes often exhibit a form of “split ownership”, in which the collection owns the state of the collection infrastructure, but client code owns the objects stored in the collection.


Servlets need not use synchronization when calling set- Attribute and getAttribute, but they may have to use synchronization when using the objects stored in the ServletContext

集合无法控制其元素的线程安全 - 客户端在使用从集合接收的元素时可能必须实现自己的同步。

由于集合无法控制其元素的线程安全性,因此它不拥有其元素的状态;元素的状态不是集合状态的子集,只有基础设施是。

关于java - 为什么对象的状态可以是对象图中以该对象为根的字段的子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com