java 开发中经常问到得懒汉模式 (单利模式)

 1 //懒汉模式
 2 class Single
 3 {
 4     public static Single s = null;
 5     public Single (){}
 6     public static Single getInstance(){
 7         if (s == null)
 8         {
 9             synchronized (Single.class){
10                 
11                 if (s == null){
12                     
13                     s = new Single ();
14                 }
15             }
16         }
17         
18         return s;
19         
20         
21     }
22     
23 
24 }

懒汉模式 (单利模式)在多线程下是不安全的,因此要加一个同步锁,锁对象用的是该类所属的字节码文件对象,仅仅一个判空是不行的,要用到两个才保险

原文地址:https://www.cnblogs.com/machao/p/4593368.html