我刚刚注意到,JDK6设置默认TimeZone的方法与JDK5不同。
以前,新的默认值将存储在线程局部变量中。在JDK6 (我刚刚回顾了1.6.0.18)中,实现已经改变了,所以如果用户可以写入"user.timezone“属性,或者如果没有安装SecurityManager,时区就会在整个VM范围内改变!否则,将发生线程本地更改。
我说错了吗?这似乎是一个相当大的变化,我在网上找不到任何关于它的东西。
下面是JDK6代码:
private static boolean hasPermission() {
boolean hasPermission = true;
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkPermission(new PropertyPermission("user.timezone", "write"));
} catch (SecurityException e) {
hasPermission = false;
}
}
return hasPermission;
}
/**
* Sets the <code>TimeZone</code> that is
* returned by the <code>getDefault</code> method. If <code>zone</code>
* is null, reset the default to the value it had originally when the
* VM first started.
* @param zone the new default time zone
* @see #getDefault
*/
public static void setDefault(TimeZone zone)
{
if (hasPermission()) {
synchronized (TimeZone.class) {
defaultTimeZone = zone;
defaultZoneTL.set(null);
}
} else {
defaultZoneTL.set(zone);
}
}
而在之前(在JDK5中),它很简单:
/**
* Sets the <code>TimeZone</code> that is
* returned by the <code>getDefault</code> method. If <code>zone</code>
* is null, reset the default to the value it had originally when the
* VM first started.
* @param zone the new default time zone
* @see #getDefault
*/
public static synchronized void setDefault(TimeZone zone)
{
defaultZoneTL.set(zone);
}
发布于 2010-02-01 22:01:21
搜索bug数据库实际上是一个很好的想法:)
http://bugs.sun.com/view_bug.do?bug_id=6352812
还有():
http://bugs.sun.com/view_bug.do?bug_id=6181786
摘要: JDK 1.5是规则的一个例外,JDK 1.6的事情回到了“正常”,根据文档,这是一个时区更改是VM范围的。
发布于 2010-02-01 21:37:09
这可能是为了修复一个bug。我会在bugs.sun.com上搜索它的基本原理。(线索也可能在中找到。)
发布于 2010-02-01 21:46:14
TimeZone.getDefault()的应用程序接口文档指出“默认TimeZone的源代码可能因实现而异”。如果您的代码依赖于标准API类的特定于实现的行为(在这种情况下,默认时区保持在线程本地级别),那么您必须预料到,使用较新版本的VM或来自不同供应商的VM时,您的代码将失败。
https://stackoverflow.com/questions/2176784
复制相似问题