<分区>
看完“Java concurrent in practice”和“OSGI in practice”后,我发现一个特定的主题非常有趣;安全出版物。以下来自JCIP:
To publish an object safely, both the reference to the object and the object's state must be made visible to other threads at the same time. A properly constructed object can be safely published by:
- Initializing an object reference from a static initializer.
- Storing a reference to it into a volatile field.
- Storing a reference to it into a final field.
- Storing a reference to it into a field that is properly guarded by a (synchronized) lock.
我的第一个问题:有多少 java 开发人员知道这个(问题)? 有多少真实世界的 Java 应用程序真正遵循这个,这真的是一个真正的问题吗?我有一种感觉,99% 的已实现的 JVM 都不是那么“邪恶”,即不能保证线程(实际上它实际上(几乎)“不可能”)看到陈旧数据只是因为引用没有遵循上面的“安全发布习语”。