除了Java有jdk,JDK类除了Javadoc之外还有其他任何规范吗?

Do JDK classes have any further specifications beyond their Javadoc? If so, where?

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值