프로그래머의 삶 Programmer's Life/Java!!

싱글톤 패턴~~

Oliver's World 2008. 12. 26. 13:00
728x90


private static Singletone instance;

public static synchronized Singletone getInstance(){

if(instance==null){

instance = new Singletone();

}

return instance;

}


 - Thread safe SingleTone pattern





public static Singleton getInstance()
{
  if (instance == null)
  {
    synchronized(Singleton.class) {
      instance = new Singleton();
    }
  }
  return instance;
}

- double - checked locking



Double-checked locking example

                  public static Singleton getInstance() {   if (instance == null)   {     synchronized(Singleton.class) {  //1       if (instance == null)          //2         instance = new Singleton();  //3     }   }   return instance; } 




참조 : http://www.ibm.com/developerworks/java/library/j-dcl.html?loc=dwmain

728x90