(单例设计模式中)懒汉式与饿汉式在多线程中的不同

9
120
121
122
123
124
125
126
127
128
129
130
131
/*
  目的:分析一下单例设计模式中,懒汉式与饿汉式在多线程中的不同!
  开发时我们一般选择饿汉式,因为它简单明了,多线程中不会出现安全问题!
  而饿汉式需要我们自己处理程序中存在的安全隐患,但是饿汉式的程序技术含量更高!
*/
/* class SinglePerson implements Runnable{
    private static SinglePerson ss = new SinglePerson("hjz", 22);//恶汉式
    private int age;
    private String name;
    private int count;
    private SinglePerson(String name, int age){
       this.age = age;
       this.name = name;
    }
    private SinglePerson(){
       age = 10;
       name = " ";
    }
    public static SinglePerson getInstance(){
       return ss;
    }
    public String getName(){
       return name;
    }
    public int getAge(){
       return age;
    }
    
    public void setIntroduceCount(){
       ++count;
    }
    
    public int getIntroduceCount(){
       return count;
    }
    
    public synchronized void run(){
         ss.setIntroduceCount();
         try{
            Thread.sleep(20);
         }catch(InterruptedException e){
            
         }
         System.out.println("this is " + ss.getName() + " " + ss.getAge() + " 被介绍的次数是:" + ss.getIntroduceCount());
     }
    
}  */
 
  class  SinglePerson  implements  Runnable{
    private  static  SinglePerson ss =  null ; //懒汉式
    private  int  age;
    private  String name;
    private  int  count;
    private  SinglePerson(String name,  int  age){
       this .age = age;
       this .name = name;
       count= 0 ;
    }
    private  SinglePerson(){
       age =  10 ;
       name =  " " ;
    }
    /*public static SinglePerson getInstance(){
       if(ss==null){//单例设计模式中,懒汉式在多线程中的缺憾啊!可能不能保证对象的唯一性
               try{
                  Thread.sleep(10);
               }catch(InterruptedException e){}
               ss = new SinglePerson("hjz", 22);
       }
       return ss;
    }*/
    
    /* public static synchronized SinglePerson getInstance(){//保证了对象的唯一性,也就是安全性保证了!但是每当调用该函数时
       if(ss==null){               //都要判断一下同步锁对象,降低了程序的效率!
               try{
                  Thread.sleep(10);
               }catch(InterruptedException e){}
               ss = new SinglePerson("hjz", 22);
       }
       return ss;
    } */
    
    public  static  SinglePerson getInstance(){ //这就是懒汉式的安全又效率的代码!
        if (ss== null ){ //这一句是必须判断的!
           synchronized (SinglePerson. class ){ //这一句只是其他的线程访问时判断
               if (ss== null ){
                       try {
                          Thread.sleep( 10 );
                       } catch (InterruptedException e){}
                       ss =  new  SinglePerson( "hjz" 22 );
               }
           }
       }
       return  ss;
    }
    
    public  String getName(){
       return  name;
    }
    public  int  getAge(){
       return  age;
    }
    
    public  void  setIntroduceCount(){
       ++count;
    }
    
    public  int  getIntroduceCount(){
       return  count;
    }
    
    public  synchronized  void  run(){
         ss.setIntroduceCount();
         System.out.println( "this is "  + ss.getName() +  " "  + ss.getAge() +  " 被介绍的次数是:"  + ss.getIntroduceCount());
     }
}
 
class  OtherThread  extends  Thread{
     public  void  run(){
         SinglePerson.getInstance().run();
     }
}
 
public  class  Test{
     public  static  void  main(String[] args){
           new  OtherThread().start();
           new  OtherThread().start();
           new  OtherThread().start();
           new  Thread(SinglePerson.getInstance()).start();
     }
}









本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/3876539.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值