Singleton pattern with combination of lazy loading and thread safety

前端 未结 5 1880
天涯浪人
天涯浪人 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-04 00:14

    Your second code snippet is, in my opinion, the best way of thread-safe lazily initializing a singleton. It actually has a pattern name

    Initialization-on-demand holder idiom

    I would suggest you use it.

提交回复
热议问题