懒汉模式和饿汉模式的区别

懒汉模式:在类加载的时候不被初始化。

饿汉模式:在类加载时就完成了初始化,但是加载比较慢,获取对象比较快。

饿汉模式是线程安全的,在类创建好一个静态对象提供给系统使用,懒汉模式在创建对象时不加上synchronized,会导致对象的访问不是线程安全的。

synchronized  关键字,代表这个方法加锁,相当于不管哪一个线程(例如线程A),运行到这个方法时,都要检查有没有其它线程B(或者C、 D等)正在用这个方法(或者该类的其他同步方法),有的话要等正在使用synchronized方法的线程B(或者C 、D)运行完这个方法后再运行此线程A,没有的话,锁定调用者,然后直接运行。它包括两种用法:synchronized 方法和 synchronized 块。
Java语言的关键字,可用来给对象和方法或者代码块加锁,当它锁定一个方法或者一个代码块的时候,同一时刻最多只有一个线程执行这段代码。当两个并发线程访问同一个对象object中的这个加锁同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。然而,当一个线程访问object的一个加锁代码块时,另一个线程仍可以访问该object中的非加锁代码块。
public class Singleton {
    /**
     * 双重锁校验机制     *
     */
    /*private static volatile Singleton instance=null;

    private Singleton() {
        System.out.println("产生一个学习委员");
    }
    public static synchronized Singleton getInstance() {
        if (instance==null){
            instance=new Singleton();
        }else {
            System.out.println("已经有一个学习委员,不能产生新的学习委员");
        }
        return instance;
    }

    public static void setInstance(Singleton instance) {
        Singleton.instance = instance;
    }
    public void getName(){
        System.out.println("我是学习委员:李远远");
    }*/
    /**
     * 懒汉 线程不安全
     */
    /*private static Singleton instance;

    public Singleton() {
        System.out.println("产生一个学习委员");
    }

    public static Singleton getInstance() {
        if(instance==null){
            instance=new Singleton();
        }else {
            System.out.println("已经有一个学习委员,不能产生新的学习委员");
        }
        return instance;
    }
    public void getName(){
        System.out.println("我是学习委员:李远远");
    }*/
    /**
     * 懒汉 线程安全
     */
    /*private static Singleton instance;

    public Singleton() {
        System.out.println("产生一个学习委员");
    }

    public static synchronized Singleton getInstance() {
        if(instance==null){
            instance=new Singleton();
        }else {
            System.out.println("已经有一个学习委员,不能产生新的学习委员");
        }
        return instance;
    }
    public void getName(){
        System.out.println("我是学习委员:李远远");
    }*/

    /**
     * 饿汉
     */
   /* private static Singleton instance=new Singleton();

    public Singleton() {
        System.out.println("产生一个学习委员");
    }

    public static Singleton getInstance() {
        if(instance==null){
            return instance;
        }else {
            System.out.println("已经有一个学习委员,不能产生新的学习委员");
            return instance;
        }

    }
    public void getName(){
        System.out.println("我是学习委员:李远远");
    }*/
    /**
     * 饿汉的 变异版
     */
    /*private static Singleton instance=null;

    static {
        instance=new Singleton();
    }
    public static Singleton getInstance() {
        if(instance==null){
            return instance;
        }else {
            System.out.println("已经有一个学习委员,不能产生新的学习委员");
            return instance;
        }

    }
    public void getName(){
        System.out.println("我是学习委员:李远远");
    }*/

    /**
     * 内部静态类
     */

    private static class SingletonHolder{
        private static final Singleton instance=new Singleton();
    }
    public Singleton() {
        System.out.println("产生一个学习委员");
    }
    public static final Singleton getInstance() {
        if(SingletonHolder.instance==null){
            return SingletonHolder.instance;
        }else {
            System.out.println("已经有一个学习委员,不能产生新的学习委员");
            return SingletonHolder.instance;
        }
    }
    public void getName(){
        System.out.println("我是学习委员:李远远");
    }

}



public class Driver {
    public static void main(String[] args) {
        Singleton singleton=Singleton.getInstance();
        singleton.getName();
        System.out.println("*****************************");
        Singleton singleton1=Singleton.getInstance();
        singleton1.getName();
        if(singleton==singleton1){
            System.out.println("same");
        }else {
            System.out.println("not same");
        }
    }
}

 

转载于:https://www.cnblogs.com/caiyu5/p/9710596.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值