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 }    

 

posted @ 2015-09-06 15:28  邻家小书童  阅读(189)  评论(0编辑  收藏  举报