单例模式

单例模式

懒汉模式

public class Lazy{
    private volatile static Lazy lazy;
    private Lazy(){

    }
    public  static Lazy getLazy() throws InterruptedException {
        if (lazy==null){
            synchronized (Lazy.class) {
                if (lazy == null) {
                    lazy = new Lazy();
                }
            }
        }
        return lazy;
 }
}
测试类
public class LazySingleton {
    public static void main(String[] args) {
       new Thread(()->{
           try {
               Lazy lazy = Lazy.getLazy();
               System.out.println(lazy);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }).start();
       new Thread(()->{
           try {
               Lazy lazy2 = Lazy.getLazy();
               System.out.println(lazy2);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }).start();

    }
}

饿汉模式

public class Hungry{
    private static Hungry hungry = new Hungry();
    private Hungry(){

    }
    public static Hungry getHungry(){
        return hungry;
    }
}
测试类
public class HungrySingeton {
    public static void main(String[] args) {
        new Thread(()->{
            System.out.println(Hungry.getHungry());
        }).start();
        new Thread(()->{
            System.out.println(Hungry.getHungry());
        }).start();
    }
}

静态内部类

public class Inside{
    private static class InsideHolder{
        private static Inside inside = new Inside();
    }
    private Inside(){

    }
    public static Inside getInside(){
        return InsideHolder.inside;
    }
}
测试类
public class InsideSingleton {
    public static void main(String[] args) {
        new Thread(()->{
            System.out.println(Inside.getInside());
        }).start();
        new Thread(()->{
            System.out.println(Inside.getInside());
        }).start();
    }
}

利用反射破坏单例

以静态内部类为例
public class Inside{
    private static class InsideHolder{
        private static Inside inside = new Inside();
    }
    private Inside(){

    }
    public static Inside getInside(){
        return InsideHolder.inside;
    }
}
测试类
public class InsideSingleton {
    public static void main(String[] args) throws Exception{
        Constructor<Inside> constructor = Inside.class.getDeclaredConstructor();
        constructor.setAccessible(true);
        Inside inside = constructor.newInstance();
        Inside inside1 = Inside.getInside();
        System.out.println(inside==inside1);//false
    }
}

枚举实现单例

public enum Enum{
    ENUM;
    public void print(){
        System.out.println(this.hashCode());
    }
}
测试类
public class EnumSingleton {
    public static void main(String[] args) {
        Enum e1 = Enum.ENUM;
        Enum e2 = Enum.ENUM;
        System.out.println(e1==e2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值