单例模式(Singleton)实现

单例模式(Singleton)实现

方法一:

//懒汉式
//单线程,多线程出错
class Singleton1 {
    private static Singleton1 instance = null;
    public Singleton1 (){
    }
    public static Singleton1 getInstance (){
        if(instance == null)
            instance = new Singleton1();
        return instance;
    }
}

方法二:

//synchronized实现多线程,效率低下
class Singleton2 {
    private static Singleton2 instance = null;
    public Singleton2 (){
    }
    public static synchronized Singleton2 getInstance (){
        if(instance == null)
            instance = new Singleton2();
        return instance;
    }
}

方法三:

//懒汉式,synchronized多线程,减小粒度,双重检测
class Singleton3 {
    private static Singleton3 instance = null;
    public Singleton3 (){
    }
    public static Singleton3 getInstance (){
        if(instance == null){
        //开始时,多个线程同时到达时,都可以进入到此处
            synchronized(Singleton3.class){
                //在创建实例前,二次检测
                if(instance == null )
                  instance = new Singleton3();
            }
        }

        return instance;
    }
}

方法四:

//饿汉式
class Singleton4 {
    //将类的实例设置为静态属性,这种方式比较占用内存,实例一直存在
    private static final Singleton4 instance = new Singleton4();
    public  Singleton4 (){  }
    public static Singleton4 getInstance(){

        return instance;
    }
}

方法五:

//懒汉式,静态内部类,按需创建对象
class Singleton5 {

    public  Singleton5 (){  }
    public static Singleton5 getInstance(){

        return Instance.instance;
    }
    private static class Instance{
        private static Singleton5 instance = new Singleton5();
    }
}

模拟多线程调用:

public class Singleton {
    public static void main(String[] args) {
		  test1();
          test2();
          test3();
          test4();
          test5();
    }

    public static void test1() {
        int i = 0;
        while(i++ < 10){
            new Thread(()->{
                System.out.println(Singleton1.getInstance());
            }).start();
        }
    }
    public static void test2() {
        int i = 0;
        while(i++ < 10){
            new Thread(()->{
                System.out.println(Singleton2.getInstance());
            }).start();
        }
    }
    public static void test3() {
        int i = 0;
        while(i++ < 10){
            new Thread(()->{
                System.out.println(Singleton3.getInstance());
            }).start();
        }
    }
    public static void test4() {
        int i = 0;
        while(i++ < 10){
            new Thread(()->{
                System.out.println(Singleton4.getInstance());
            }).start();
        }
    }
    public static void test5() {
        int i = 0;
        while(i++ < 10){
            new Thread(()->{
                System.out.println(Singleton5.getInstance());
            }).start();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值