问题描述
我正在阅读有关Singleton模式的Wiki,我不确定是否理解这一点: https://en.wikipedia.org/wiki/initialization-on-demand_holder_idiom 正确的一部分.
使它变得简单:为什么Bill Pugh的解决方案比上面的示例更好?
是因为静态类在实际使用或类似的东西之前没有加载静态类,因此我们在转到getInstance()方法之前不创建对象? 此方法线程也仅在初始化对象的范围内安全吗?
推荐答案
我认为PUGH先生的版本被高度重视,因为它仅在称为getInstance()时执行单例的实例化.如果您的Singleton Construction做一些代价高昂的事情,那么这对您来说可能是一个优势.如果您就像世界大多数人只是为了避免静态方法(并且还没有进入依赖注入框架),那么我不会为此而失去任何睡眠.
正如文章所述,PUGH先生的方法比静态实例变量更宽松 - 但实际上,如果Singleton类已加载,您将无论如何都会调用Getinstance方法.因此,作为计算机科学练习,它很有用,但是在现实世界中,其好处是有争议的.
P.S.我不太在意布洛赫先生在这里使用枚举的榜样,就是说我的singleton is-a枚举对我来说并不适合我(尤其是从正确地说,从不实现界面的人获取常数)
其他推荐答案
JLS保证只有在第一次使用时才加载一类(使单例初始化懒惰),并且类加载是线程安全(制作getInstance()方法也可以螺纹安全)
关于为什么线程安全
因为第一次调用了getInstance(),因此JVM将加载持有器类.如果另一个线程同时调用getInstance(),JVM不会第二次加载持有人类:它将等待第一个线程完成类加载,以及在持有人类的加载和初始化结束时,这两个线程都会看到持有人类的类别正确初始化,从而包含唯一的Singleton实例.
其他推荐答案
是因为静态类不是 在VM实际上是由VM加载的 二手
不仅是静态类,任何类.课程直到引用后才加载.请参阅JLS- 12.4.1何时初始化发生
或类似的东西,所以我们不 在我们转向之前创建对象 getInstance()方法?
正好.
也是该方法线程仅安全 在初始化 对象?
分发参考是线程安全的,因此此方法始终是线程安全,不仅在创建时间
问题描述
I'm reading wiki about the singleton pattern and I'm not sure if I understand this: https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom part of it correctly.
So to make it simple: Why is Bill Pugh's solution better than the example above?
Is it because a static class is not load by the VM before it's actually used or something like this, so we don't create the object before we turn to the getInstance() method? Also is that method thread safe only to the extent of initializing the object?
推荐答案
I think Mr Pugh's version is held in high regard because it only performs the instantiation of the singleton when getInstance() is called i.e. not when the class (the class holding the getInstance method) is loaded. If your singleton construction does something costly then this may be an advantage for you. If you're like the majority of the world whose singletons are just to avoid static methods (and you haven't moved onto dependency injection frameworks), then I would not lose any sleep over it.
As the article states, Mr Pugh's method is lazier than the static instance variable - but in reality if the Singleton class gets loaded you're going to be calling the getInstance method anyhow. So as a computer science exercise it's useful, but in the real world its benefits are debatable.
p.s. I don't care much for Mr Bloch's example here as to use an enum would be to say My Singleton IS-A enum, which doesn't sound right to me (especially from someone who, rightly, says never implement an interface just to get the constants)
其他推荐答案
The JLS guarantees that a class is only loaded when it's used for the first time (making the singleton initialization lazy), and that the class loading is thread-safe (making the getInstance() method thread-safe as well)
As for why thread-safe
Because the first time getInstance() is called, the JVM will load the holder class. If another thread calls getInstance() concurrently, the JVM won't load the holder class a second time: it will wait for the first thread to have completed the class loading, and at the end of the loading and initialization of the holder class, both thread will see the holder class properly initialized and thus containing the unique singleton instance.
其他推荐答案
Is it because a static class is not load by the VM before it's actually used
Not just a static class, any class. Classes aren't loaded until they are referenced. See the JLS - 12.4.1 When Initialization Occurs
or something like this, so we don't create the object before we turn to the getInstance() method?
Exactly.
Also is that method thread safe only to the extent of initializing the object?
Handing out a reference is thread-safe, so this method is always thread-safe, not just at creation time