Do JDK classes have any further specifications beyond their Javadoc?

限于喜欢 提交于 2019-12-05 02:44:35

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.)

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.

In the end there's just a continuum from will never change over could change behavior in the next point release to just happens to work on your platform. Even specified behavior can be deprecated and then removed at some point, it just happens extremely rarely (e.g. Thread.destroy). So whether you can rely on reasonable-but-unspecified behavior depends how strong a guarantee you need, how much effort you want to spend on coding around things defensively / adding tests to ensure you detect future changes etc.

But yes, the javadocs are the strongest guarantee you can get, everything else means moving onto thinner ice.

Many projects rely on APIs that are not only undocumented but considered internal and ostensibly implementation-specific. sun.misc.Unsafe being the prime example here, to the point where most of its features became reified as proper JDK APIs in 9.

In the case of Collections.unmodifiableMap if you wanted to be really defensive about safe publication you could insert a store fence after creating it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!