Singleton pattern with combination of lazy loading and thread safety

前端 未结 5 1879
天涯浪人
天涯浪人 2021-02-03 23:53

I was doing some research about singletons, specifically regarding lazy vs eager initialization of singletons.

An example of eager initialization:

public         


        
5条回答
  •  不思量自难忘°
    2021-02-03 23:57

    You first design is actually lazy. Think about it, the instance is only created when the class is initialized; the class is only initialized when the getSingleton() method is called [1]. So the instance is only created when it's asked for, i.e. it's lazily created.

    [1] http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.1

提交回复
热议问题