Java基础-单例模式

单例模式:

优缺点及使用场景

使用场景

  • 全局统一使用的配置类

  • 线程池

  • 方便多个线程直接之间通信的类

  • 数据库连接池

优缺点

设计要点

  • 约束只有一个实力,外面不能随意的创建:构造器私有化

  • 保存实例:用静态变量保存

  • 为外面提供此实力:用get方法

饿汉式-懒汉式

饿汉式

饿汉式不存在线程安全的问题

最简单
public class Singleton1 {

    // 私有化构造方法,其他人无法创建实例
    private Singleton1(){};

    // 外部可以访问
    public static final Singleton1 SINGLETON = new Singleton1();

}
枚举
// 饿汉式-枚举类型 单例模式
public enum Singleton2 {

    INSTANCE

}

调用

public class TestMain {
    public static void main(String[] args) {
        Singleton2 singleton2 = Singleton2.INSTANCE;
        System.out.println(singleton2);
    }
}

静态代码块

当有属性需要初始化时

// 饿汉式-静态代码块类型 单例模式
public class Singleton3 {
    private Singleton3(){};

    public static final Singleton3 INSTANCE ;

    static{
        INSTANCE= new Singleton3();
    }
}
静态代码块

加载配置文件

import java.io.IOException;
import java.util.Properties;

// 饿汉式-静态代码块类型 单例模式
public class Singleton3 {
    private Singleton3(){};
    private String info;
    private Singleton3(String info){
        this.info = info;
    }
    public static final Singleton3 INSTANCE ;
    static{
        try {
            Properties properties = new Properties();
            properties.load(Singleton3.class.getClassLoader().getResourceAsStream("single.properties"));
            INSTANCE= new Singleton3(properties.getProperty("info"));

        } catch (IOException e) {
            throw  new RuntimeException();
        }
    }

    public String getInfo() {
        return info;
    }

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

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

懒汉式

单线程

存在线程安全问题

// 懒汉式,延迟创建实例对象
public class Singleton4 {
    private static Singleton4 instance;
    private Singleton4(){};
    public static Singleton4 getInstance() throws InterruptedException {
        if (null == instance){
            Thread.sleep(100);
            instance = new Singleton4();
        }
        return instance;
    }
}

测试

import java.util.concurrent.*;
public class TestMain {
    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 ex = Executors.newFixedThreadPool(2);
        Future<Singleton4> f1 = ex.submit(c);
        Future<Singleton4> f2 = ex.submit(c);
        Singleton4 s1 = f1.get();
        Singleton4 s2 = f2.get();
        System.out.println(s1);
        System.out.println(s2);
    }
}
线程安全版
// 懒汉式,延迟创建实例对象
public class Singleton5 {
    private static Singleton5 instance;
    private Singleton5(){};
    public static Singleton5 getInstance() throws InterruptedException {
        if (null == instance){
            synchronized(Singleton5.class){
                if (null == instance){
                    Thread.sleep(100);
                    instance = new Singleton5();
                }
            }
        }
        return instance;
    }
}
内部类
// 懒汉式,延迟创建实例对象
public class Singleton6 {

    private Singleton6(){};

    // 静态内部类不会随着外部类的初始化而初始化。
    // 当用这个类的时候才会加载初始化
    public static class Inner{
        private static final Singleton6 INSTANCE = new Singleton6();
        public static Singleton6 getInstance(){
            return INSTANCE;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值