java - 在简单单例实例化中获取 ExceptionInInitializerError

标签 java exception static singleton initializer

我一定是在做一些非常愚蠢的事情,但是当我尝试在我的单例中实例化一个对象时,我得到了一个 ExceptionInInitializerError:

class MySingleton {

  private static MySingleton instance = null;
  private OtherObject obj;

  // Do I instantiate obj here???
  private MySingleton() {
    //obj = new OtherObject();
  }

  // Or here?
  {
    //obj = new OtherObject();
  }

  // Or where? ...

  public static MySingleton getInstance() {
    if (instance == null)
      instance = new MySingleton();
    return instance;
  }

}

我应该在构造函数中创建另一个对象,还是对于单例来说它总是应该为空?我在构造函数和初始化程序 block 中都遇到了异常...

这是主要的():

public static void main(String[] args) {
    try {
        MySingleton singleton = MySingleton.getInstance();
    } catch (Error e) {
        System.err.println("Error!");
    }
}

最佳答案

您可以通过消除 lazy initialization 来避免一些混淆。风险(您目前正在为异常(exception)情况付费)。由于您实际上只是返回静态实例,为什么不在运行时创建该静态实例(即,不要等待 null):

class MySingleton {

  private static MySingleton instance = new MySingleton();

  // You could do this here or in the constructor
  // private OtherObject obj = new OtherObject();

  /** Use this if you want to do it in the constructor instead. */
  private OtherObject obj;

  private MySingleton() {
      obj = new OtherObject();
  }

  /** Now you can just return your static reference */
  public static MySingleton getInstance() {
      return instance;
  }

}

如果您注意到,现在一切都是确定性和直接的。您的 MySingleton.instance 在运行时填充并通过静态方法 MySingleton.getInstance() 访问。

我意识到这与原始 GOF 设计模式书中的确切模式不匹配,但您会注意到该类的用法实际上是相同的。

编辑:跟进其他答案和评论中提出的一些线程安全点,我将尝试说明问题和 GOF 书中最初提出的解决方案是如何非线程安全。如需更好的引用,请参阅 Java Concurrency in Practice我拥有并放在我的 ACM/Safari 在线书架上。坦率地说,这是一个比我草拟的例子更好的来源,但是,嘿,我们只能努力....

因此,假设我们有两个名为 Thread1 和 Thread2 的线程,巧合的是,每个线程同时调用 MySingleton.getInstance() 方法。这在现代多核超线程系统中是完全可能的。现在,即使这两个线程恰好同时命中了该方法的入口点,也无法保证哪一个会命中任何特定的语句。所以,你可以看到这样的东西:

// Note that instance == null as we've never called the lazy getInstance() before.

Thread1: if (instance == null)
Thread2: if (instance == null) // both tests pass - DANGER
Thread1:     instance = new MySingleton();
Thread2:     instance = new MySingleton(); // Note - you just called the constructor twice
Thread1: return instance; // Two singletons were actually created 
Thread2: return instance; // Any side-effects in the constructor were called twice

if-null 测试中说明的问题称为 race condition .你无法知道谁会在什么时候发表什么声明。结果,这就像两个线程都在互相竞争悬崖。

如果您查看我的示例的相同类型检查,其中两个线程仍然同时调用 getInstance(),您会看到类似这样的内容:

Thread1: return instance;
Thread2: return instance;

是的,很简单,但这就是我的观点。该对象在很久以前构造过一次,并且线程可以指望它的值是一致的。不存在种族。

注意:您仍然需要担心 OtherObject 的构造函数的内容。教训是:并发很难。如果您正在使您的构造函数线程安全(您应该这样做),请确保 OtherObject 的作者以同样的礼貌对待您。

关于java - 在简单单例实例化中获取 ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3991863/

相关文章:

c# - WCF 方法抛出 SerializationException

javascript - Immutable.Map.deleteAll() 不是函数

java - 为什么Hamcrest `matches`方法使用Object作为输入类型而不使用泛型类型作为输入

java - Ant JUnit ClassNotFoundException

rest - 无法捕获从另一个函数抛出的异常

c++ - 在 C++ 中使用复合文字

java - 使用 ClassName.method() 时,Compiler、Static 或实例方法首先查看哪个方法?

Java,静态变量不会从类外部初始化

java - 计算数组中的元素个数

java - Android中的mp3标题读取问题