Java实现单例模式

1,单例模式

解法一:只适用于单线程:

package com.singleton;

import sun.security.jca.GetInstance.Instance;

public class Singleton1 {

    private static Singleton1 instance = null;
    public Singleton1() {
        
    }
    
    public static Singleton1 getInstance() {
        if (instance == null) {
            instance = new Singleton1();
        }
        return instance;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Singleton1 s1 = getInstance();
        Singleton1 s2 = getInstance();
        System.out.println(s1==s2);
    }

}

 

解法二:适用于多线程但效率不高----在getInstance()上加synchronized加锁

package com.singleton;

public class Singleton1 {

    private static Singleton1 instance = null;
    public Singleton1() {
        
    }
    
    public static synchronized Singleton1 getInstance() {
        if (instance == null) {
            instance = new Singleton1();
        }
        return instance;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Singleton1 s1 = getInstance();
        Singleton1 s2 = getInstance();
        System.out.println(s1==s2);
    }

}

解法三:改进的多线程单例----不需要整个方法加锁,只需要创建时加锁

package com.singleton;


public class Singleton1 {

    private static Object lock = new Object();
    private static Singleton1 instance = null;
    public Singleton1() {
        
    }
    
    public static Singleton1 getInstance() {
        if (instance == null) {
            synchronized (lock) {
                if (instance == null) {
                    instance = new Singleton1();
                }
            }
            
        }
        return instance;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Singleton1 s1 = getInstance();
        Singleton1 s2 = getInstance();
        System.out.println(s1==s2);
    }

}

 

解法四(强烈推荐):利用静态构造函数

package com.singleton;

public class Singleton2 {

    private static Singleton2 instance = new Singleton2();
    
    public static Singleton2 getInstance() {
        return instance;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Singleton2 s1 = getInstance();
        Singleton2 s2 = getInstance();
        System.out.println(s1 == s2);
    }

}

原理:类成员变量只在类加载时初始化一次。

 

解法五(强烈推荐):

package com.singleton;

public class Singleton3 {

    private static class SingletonHolder {
        private final static Singleton3 instance = new Singleton3();
    }
    
    public Singleton3() {
    }
    
    public static Singleton3 getInstance() {
        return SingletonHolder.instance;
    }
    
    public static void main(String[] args) {
        Singleton3 s1 = getInstance();
        Singleton3 s2 = getInstance();
        System.out.println(s1 == s2);

    }

}

因为SingletonHolder内部类只有在显式被调用时才会加载,所以只有在调用getInstance方法时才会实例化instance,这样就解决了解法四加载过早的问题。

转载于:https://www.cnblogs.com/IvySue/p/7529001.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值