java多线程单例模式:
1 public static Singleton getInstance(){
2 if (instance == null)
3 synchronized(instance){
4 if(instance == null)
5 instance = new Singleton();
6 }
7 return instance;
8 }
另一个改进:
复制代码
1 public class Singleton{
2 private Singleton(){
3 …
4 }
5 private static class SingletonContainer{
6 private static Singleton instance = new Singleton();
7 }
8 public static Singleton getInstance(){
9 return SingletonContainer.instance;
10 }
11 }
复制代码