Several elegant implementations of thread-safe singleton pattern

There are several ideas for the implementation of thread-safe singleton mode. Personally, I think the second solution is the most elegant:
1. Hungry style
2. List content
3. With the help of inner classes
4. Common locking solution
5. Double detection, but pay attention to the way of writing

If the singleton pattern continues to expand to the N-ary singleton pattern, it is the object pool pattern.
Hungry Chinese-style singleton

public class Singleton {
   private final static Singleton INSTANCE = new Singleton();

   private Singleton() { }

   public static Singleton getInstance() {
      return INSTANCE;
   }
}

With inner class

public class Singleton {

   private Singleton() { }

   private static class SingletonHolder {
      private final static Singleton INSTANCE = new Singleton();
   }

   public static Singleton getInstance() {
      return SingletonHolder.INSTANCE;
   }
}

It is a lazy singleton, because the Java mechanism stipulates that the inner class SingletonHolder will only be loaded when the getInstance() method is called for the first time (lazy is implemented), and its loading process is thread-safe. The instance is instantiated once when the inner class is loaded.

common lock solution

public class Singleton {
   private static Singleton instance = null;

   private Singleton() { }

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

      return instance;
   }
}

Although the thread safety problem is solved, each thread calls getInstance to be locked. We want to lock only when the getInstance is called for the first time. Please see the following double detection scheme

Dual detection scheme

public class Singleton {
   private static Singleton instance = null;

   private Singleton() { }

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

      return instance;
   }
}

由于指令重排序问题,所以不可以直接写成下面这样:
public class Singleton {
   private static Singleton instance = null;

   private Singleton() { }

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

      return instance;
   }
}

但是如果instance实例变量用volatile修饰就可以了,volatile修饰的话就可以确保instance = new Singleton();对应的指令不会重排序,如下的单例代码也是线程安全的:
public class Singleton {
   private static volatile Singleton instance = null;

   private Singleton() { }

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

      return instance;
   }
}

Original http://www.oschina.net/code/snippet_111708_25417

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643100&siteId=291194637