设计模式-单例模式

单例模式

饿汉式

  1. 私有构造方法
  2. static 静态new一个对象返回

优点: 简单,线程安全

缺点:类加载的时候,new一个对象,如果不使用,就属于浪费内存

/**
 * 饿汉模式
 */
public class EHan {
    /*静态new独享*/
    private static EHan instance = new EHan();/*私有构造参数*/
    private EHan() {
    }
    public static EHan getInstance(){
        return instance;
    }
    public static void main(String[] args) {
        EHan instance = EHan.getInstance();
        EHan instance2 = EHan.getInstance();
        System.out.println(instance);
        System.out.println(instance2);
    }
}

饿汉式变种

public class Singleton {
    private Singleton(){
    }   
    public static enum SingletonEnum {
        SINGLETON;
        private Singleton instance = null;
        private SingletonEnum(){
            instance = new Singleton();
        }
        public Singleton getInstance(){
            return instance;
        }
    }
}

饿汉式-静态内部类

public class Singleton {  
	// 静态内部类
    private static class SingletonHolder {  
    	private static final Singleton INSTANCE = new Singleton();  
    }  
    private Singleton (){}  
	
    public static final Singleton getInstance() {  
    	return SingletonHolder.INSTANCE;  
    }  
}  

懒汉式1.0版本

  1. 私有构造方法
  2. return实例的时候,判断是否null 再new对象

优点:1.使用的时候,在创建对象,节省资源

缺点:1.线程不安全

import sun.nio.ch.ThreadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class Lanhan {
    private static Lanhan instance;
    /*私有构造函数*/
    private Lanhan() {
    }
    /*返回实例*/
    public static Lanhan getInstance(){
        /*判断实例是否为null*/
        if (null == instance) {
            instance = new Lanhan();
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 50000; i++) {
            Runnable runnable = ()->{
                System.out.println(Lanhan.getInstance());
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();
        }
    }
}

懒汉式2.0

  1. 私有构造方法
  2. return实例的时候,使用sysnchronized,判断是否null 再new对象

优点:1.使用的时候,在创建对象,节省资源,线程安全

缺点:1.性能不好

public class Lanhan2 {
    private static Lanhan2 instance;
    /*私有构造函数*/
    private Lanhan2() {
    }
    /*返回实例 添加同步关键词*/
    public synchronized static Lanhan2 getInstance(){
        /*判断实例是否为null*/
        if (null == instance) {
            instance = new Lanhan2();
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 50000; i++) {
            Runnable runnable = ()->{
                System.out.println(Lanhan2.getInstance());
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();
        }
    }
}

懒汉式3.0 双检锁

  1. 私有构造方法
  2. return实例的时候,使用sysnchronized锁住类对象,判断是否null 再new对象

优点:1.使用的时候,在创建对象,节省资源,线程安全

缺点:1.性能不好

/**
 * 双检锁
 */
public class DoubleCheck {
    private DoubleCheck() {
    }
    /**防止指令重排**/
    private volite static DoubleCheck instance;
    public static DoubleCheck getInstance(){
        if (null == instance) {
            synchronized (DoubleCheck.class){
                if (null == instance){
                    instance=new DoubleCheck();
                }
            }
        }
        return instance;
    }
    public static void main(String[] args) {
        DoubleCheck instance = DoubleCheck.getInstance();
        DoubleCheck instance2 = DoubleCheck.getInstance();
        System.out.println(instance);
        System.out.println(instance2);
    }
}

❤推荐方式 枚举

优点:1.使用的时候,在创建对象,节省资源,线程安全

public class Singleton {
    private Singleton(){
    }   
    public static enum SingletonEnum {
        SINGLETON;
        private Singleton instance = null;
        private SingletonEnum(){
            instance = new Singleton();
        }
        public Singleton getInstance(){
            return instance;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值