欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

singleton pattern

程序员文章站 2024-03-20 20:26:58
...

1 normal mode

class Singleton
{
    private Singleton(){};

    Singleton singleton;
    static Singleton getInstance() {
    if(singleton == null)
      singleton = new Singleton(); 
   return singleton;   
   }
}

not thread safe.

Thread safe version:

class Singleton
{
    static private Singleton(){};

    Singleton singleton;
    static Singleton getInstance() {
    if(singleton == null)
      singleton = new Singleton(); 
   return singleton;   
   }
}