Java单例模式

 1 package shb.java.testThread;
 2 
 3 public class demo1 {
 4     public static void main(String[] args) {
 5         Type type = Type.getSingleType();
 6         Type_2 type_2 = Type_2.getSinType_2();
 7     }
 8 }
 9 //懒汉式单例模式
10 class Type{
11     private  static Type type = null;
12     private Type(){
13         
14     }
15     public static Type getSingleType(){
16             if(type==null){
17                 synchronized (Type.class) {
18                 if(type==null){
19                     type = new Type();
20                 }
21               }    
22             }
23             return type;            
24     }
25     
26 }
27 //饿汉式单例模式
28 class Type_2{
29     private static Type_2 type_2 = new Type_2();
30     private Type_2(){
31         
32         
33     }
34     public static Type_2 getSinType_2(){
35         
36         return type_2;
37     }
38     
39 }    
吾宁做一叶扁舟,始航于湖边,遨游于海上,浪迹于江中。
原文地址:https://www.cnblogs.com/assassin666/p/4785735.html