当为高度并发的单例类研究单元测试时,我偶然发现了以下奇怪的行为(在JDK 1.8.0_162上进行了测试):
private static class SingletonClass {
static final SingletonClass INSTANCE = new SingletonClass(0);
final int value;
static SingletonClass getInstance() {
return INSTANCE;
}
SingletonClass(int value) {
this.value = value;
}
}
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
System.out.println(SingletonClass.getInstance().value); // 0
// Change the instance to a new one with value 1
setSingletonInstance(new SingletonClass(1));
System.out.println(SingletonClass.getInstance().value); // 1
// Call getInstance() enough times to trigger JIT optimizations
for(int i=0;i<100_000;++i){
SingletonClass.getInstance();
}
System.out.println(SingletonClass.getInstance().value); // 1
setSingletonInstance(new SingletonClass(2));
System.out.println(SingletonClass.INSTANCE.value); // 2
System.out.println(SingletonClass.getInstance().value); // 1 (2 expected)
}
private static void setSingletonInstance(SingletonClass newInstance) throws NoSuchFieldException, IllegalAccessException {
// Get the INSTANCE field and make it accessible
Field field = SingletonClass.class.getDeclaredField("INSTANCE");
field.setAccessible(true);
// Remove the final modifier
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
// Set new value
field.set(null, newInstance);
}
main()方法的最后两行在INSTANCE的值上存在分歧-我猜是JIT完全放弃了该方法,因为该字段是static final。删除final关键字会使代码输出正确的值。
抛开对单身人士的同情(或缺乏同情心),忘记一分钟使用这样的反射会带来麻烦-我的假设是正确的,因为应该归咎于JIT优化?如果是这样-那些字段仅限于静态最终字段吗?
static final
字段的类。除此之外,此反射hack是否由于JIT或并发而中断也没关系。