单例设计模式

##单例设计模式

单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

注意:

  • 1、单例类只能有一个实例。

  • 2、单例类必须自己创建自己的唯一实例。

  • 3、单例类必须给所有其他对象提供这一实例

    单例模式的几种实现方式

饿汉式:直接创建对象,不存在线程安全

在类初始化的时候直接创建实例,不管你是否要需要这个对象都会创建

这里例举3种: 直接实例化、枚举式、静态代码块

  • 直接实例化(直观)
public class Singlenton {

    public static final Singlenton INSTANCE = new Singlenton();

    private Singlenton(){
        }
    public static void test(){

    }
  • 枚举式(最简洁)
/**
* 枚举类型:表示该类 型的对象是有限的几个
 * 我们可以先定一个,就成了单例
 */
public enum Singleton2 {
    INSTANCE
}

调用测试(直接实例化 枚举式 )

public class Test {
    public static void main(String[] args) {

        Singlenton s  = Singlenton.INSTANCE;
        System.out.println(s);

        Singleton2 s2  = Singleton2.INSTANCE;
        System.out.println(s2);
    }
}

测试结果

ssingle.Singlenton@1b6d3586
  
INSTANCE
  • 静态代码块(适合复杂的实例化)

public class Singleton3 {
    public static final Singleton3 INSTANCE;
    private String info;
    static {
        try {
            Properties pro = new Properties();
            //用类加载其加载
            pro.load(Singleton3.class.getClassLoader().getResourceAsStream("single.properties"));
            INSTANCE = new Singleton3(pro.getProperty("info"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 构造器私有化
     */
    private Singleton3(String info){
        this.info=info;
    }

    public static Singleton3 getINSTANCE() {
        return INSTANCE;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Singleton3{" +
                "info='" + info + '\'' +
                '}';
    }
}

single.properties

info=test

测试

public class Test3 {
    public static void main(String[] args) {
        Singleton3 s = Singleton3.INSTANCE;
        System.out.println(s);
    }
}

测试结果

Singleton3{info='test'}

懒汉式:延迟创建对象

/**
 * 懒汉式
 *      延迟创建这个方法
 *
 * 1构造器私有化
 * 2用一个静态方法保存这个实例对象
 * 3提供一个静态方法,获取这个对象
 */
  • 线程不安全(适合于单线程)

    1).单线程时

public class Singleton4 {
    private static Singleton4 instance;

    private Singleton4(){

    }
    public static Singleton4 getInstance(){
        if(instance == null){
            instance = new Singleton4();
        }
        return instance;
    }
}

单线(单线程)

public class Test4 {
    public static void main(String[] args) {
        Singleton4 s1  = Singleton4.getInstance();
        Singleton4 s2  = Singleton4.getInstance();
        System.out.println(s1 == s2);
        System.out.println(s1);
        System.out.println(s2);
    }
}

结果

true
single.Singleton4@1b6d3586
single.Singleton4@1b6d3586

2).多线程时

public class Singleton4 {
    private static Singleton4 instance;

    private Singleton4(){

    }
    public static Singleton4 getInstance(){
        if(instance == null){
          /**
           *  第一个线程进来遇到这里会线程阻塞
           *阻塞后cpu会让出来,第二个线程进来,由于第一个还没有没 instance = new Singleton4();
           *当这个线程休眠结束后 第一个会执行new ,但第二个也早就进来了 也可能会执行new
           *会有可能导致出现2个new 第一个输出false  这会导致线程不安全
           */
            try {
                //线程休眠
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            instance = new Singleton4();
        }
        return instance;
    }
}

测试(多线程)

public class Test4 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Singleton4> c = new Callable<Singleton4>() {
            @Override
            public Singleton4 call() throws Exception {
                return Singleton4.getInstance();
            }
        };
        ExecutorService es = Executors.newFixedThreadPool(2);
        Future<Singleton4> f1 = es.submit(c);
        Future<Singleton4> f2 = es.submit(c);

        Singleton4 s1 = f1.get();
        Singleton4 s2 = f2.get();

        System.out.println(s1 == s2);
        System.out.println(s1);
        System.out.println(s2);

        es.shutdown();
    }
}

测试结果

false
single.Singleton4@7f31245a
single.Singleton4@6d6f6e28
  • 线程安全

1.增加synchronize使线程变得安全(在Singleton4的基础上)

public class Singleton5 {
    private static Singleton5 instance;

    private Singleton5(){

    }
    public static Singleton5 getInstance(){
        //增加效率 
        if(instance ==null) {
            //增加synchronize使线程变得安全  增加锁
            synchronized (Singleton5.class) {
                if (instance == null) {
                    try {
                        //线程休眠
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    instance = new Singleton5();
                }
            }
        }
        return instance;
    }
}

测试

public class Test5 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {


        Callable<Singleton5> c = new Callable<Singleton5>() {
            @Override
            public Singleton5 call() throws Exception {
                return Singleton5.getInstance();
            }
        };
      	//创建2个线程
        ExecutorService es = Executors.newFixedThreadPool(2);
        Future<Singleton5> f1 = es.submit(c);
        Future<Singleton5> f2 = es.submit(c);

        Singleton5 s1 = f1.get();
        Singleton5 s2 = f2.get();

        System.out.println(s1 == s2);
        System.out.println(s1);
        System.out.println(s2);

        es.shutdown();
    }
}

测试结果

true
single.Singleton5@7f31245a
single.Singleton5@7f31245a

  • 静态代码块

/**
 * @Author: FF
 * @Date: 2020/9/6 19:11
 *
 * 在内部类别初始化时,才会创建INSTANCE的实例化对象
 *静态内部类不会自动随着外部类的加载和初始化而初始化,他要单独去加载和初始化
 * 因为实在内部类加载和初始化是,创建的,因此线程是安全的
 *
 * 比第5种代码更加简洁
 */
public class Singleton6 {
    private Singleton6(){
    }
    /**
     *  内部类
     */
    private static class Inner{
        private static final Singleton6 INSTANCE = new Singleton6();
    }
    /**
     * 拿到INSTANCE
     */
    public static Singleton6 getInstance(){
        return Inner.INSTANCE;
    }
}

测试

public class Test6 {
    public static void main(String[] args) {
        Singleton6 singleton6 = Singleton6.getInstance();
        System.out.println(singleton6);
    }
}

测试结果

single.Singleton6@1b6d3586

总结

  • 饿汉式:枚举最简单
  • 懒汉式:静态代码块最简单
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值