问题描述
JDK类除了Javadoc之外还有其他任何规范吗?如果是这样,在哪里?
Do JDK classes have any further specifications beyond their Javadoc? If so, where?
例如,考虑 Collections.unmodifiableMap
。 对线程安全没有任何说明;所以,从Javadoc开始,我不能认为将结果映射暴露给其他线程是安全的,而不需要采取我自己的一些特殊步骤来获得线程安全性。但恕我直言,任何现实的实现都会将内部地图存储在 final
字段中,因此在Java 5及更高版本中,只要内部地图生成的地图就是线程安全的是(在生成的地图的任何访问与内部地图的任何先前修改之间的发生在之前关系)。这就是OpenJDK实现的功能,例如。
For example, consider Collections.unmodifiableMap
. Its Javadoc doesn't say anything about thread-safety; so just going from the Javadoc, I can't assume that it's safe to expose the resulting map to other threads without taking some special steps of my own to gain thread-safety. But IMHO, any realistic implementation would store the internal map in a final
field, so in Java 5 and higher, the resulting map will be thread-safe as long as the internal map is (with a "happens-before" relationship between any accesses of the resulting map and any previous modifications to the internal map). That's what the OpenJDK implementation does, for example.
那么,我怎么能弄清楚我是否可以假设一个给定的行为?
So, how can I figure out if I can portably assume a given behavior?
推荐答案
Javadoc是规范。也就是说,编写良好的规范是非常困难的,平衡两者并不会遗漏有用的东西而不会过度使用(并破坏未来的演化实现能力。)
The Javadoc is the specification. That said, writing good specification is excruciatingly difficult, balancing both not leaving out useful stuff with not overcommitting (and undermining future ability to evolve implementation.)
如果我不得不猜测,我会说这个被排除在规范之外的原因(除了可能的疏忽)是任何线程安全都是有条件的,而不是基础集合(a)不被发布和(b)在不可修改的视图之后不被修改已创建,这也必须仔细指定。
If I had to guess, I'd say the reason this was left out of the specification (other than possibly oversight) is that any thread-safety would be conditional than the underlying collection (a) not be published and (b) not be modified after the unmodifiable view is created, and this would have to be carefully specified as well.
这篇关于JDK类除了Javadoc之外还有其他任何规范吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!