singleton pattern

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;   
   }
}

  

 

posted @ 2014-09-17 19:49  williamwood  阅读(149)  评论(0编辑  收藏  举报