问题描述
我在 Brian Goetz 2006 年版的 Java Concurrency in Practice 中阅读了以下声明.
虽然可以编写一个线程安全程序,将其所有状态存储在公共静态字段中,但验证其线程安全性要困难得多……"(第 4 章 - 组合对象;4.1 设计线程安全类的第 1 段)
一直以来我都认为静态字段属于一个类并且不能存储对象的状态.我是不是误解了什么?
I read the following statement in Java Concurrency in Practice by Brian Goetz, 2006 edition.
"While it is possible to write a thread safe program that stores all its state in public static fields,it is a lot harder to verify its thread safety..." (Chapter4 - Composing Objects; 1st paragraph of 4.1 Designing a Thread Safe Class)
All this while I thought that static fields belong to a class and can't store an object's state. Am I misinterpreting something?
推荐答案
作者没说静态字段属于对象.他的意思是,将数据存储在类的静态字段(原因)中的程序比程序更难使其真正线程安全,......适当地使用封装."
Author said nothing about static fields belong to object.He meant that program, that stores data in static fields of classes (of cause), is much harder make it really thread safe, than program, "... that uses encapsulation appropriately."
是的,实例(对象)能够在类的静态字段中存储数据.但作者想到了另一件事.
And yes, instances(objects) are able store data in static fields of class. But author had in mind another thing.
这篇关于公共静态字段中的对象状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!