栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

设计模式-单例模式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

设计模式-单例模式

文章目录
    • 单例模式
      • 饿汉式
      • 饿汉式变种
      • 饿汉式-静态内部类
      • 懒汉式1.0版本
      • 懒汉式2.0
      • 懒汉式3.0 双检锁
      • ❤推荐方式 枚举

单例模式 饿汉式
  1. 私有构造方法
  2. static 静态new一个对象返回

优点: 简单,线程安全

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

public class EHan {
    
    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(){
        
        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(){
        
        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;
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/973581.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号