Java Singleton多种方式

Java 用enum 实现一个单列

//enum class
public eumn SingleTonEnum {
	 SINGLETON
	private SingletonClass singleton ;
	SingleTon(){
		this.singleton = new SingletonClass ()
	}
	//get singleton
	public SingletonClass getSingleTon(){
		return this.singleton ;
	}
}

//singleton class
class SingletonClass {
    int i = 0;
    public SingletonClass() {
        System.out.println("SingletonClass被初始化 " + ++i + " 次");
    }
}

//main test 
class SingletonTest {
    public static void main(String[] args) {
        SingletonClass instance1 = Singleton.INSTANCE.getInstance();
        SingletonClass instance2 = Singleton.INSTANCE.getInstance();
        System.out.println("instance1 == instance2: " + (instance1 == instance2));
    }
}

java 懒汉式实现singleton

懒汉式是延迟加载的,当需要用的时候才会创建实列,可以节省不必要的资源开销,但也存在线不安全问题
// 单例模式的懒汉实现1--线程不安全
class SingletonLazy1 {
	private static SingletonLazy1 singletonLazy;

	private SingletonLazy1() {

	}

	public static SingletonLazy1 getInstance() {
		if (null == singletonLazy) {
			try {
				// 模拟在创建对象之前做一些准备工作
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			singletonLazy = new SingletonLazy1();
		}
		return singletonLazy;
	}
}
// 单例模式的懒汉实现2--线程安全
// 通过设置同步方法,效率太低,整个方法被加锁
class SingletonLazy2 {
	private static SingletonLazy2 singletonLazy;

	private SingletonLazy2() {

	}

	public static synchronized SingletonLazy2 getInstance() {
		try {
			if (null == singletonLazy) {
				// 模拟在创建对象之前做一些准备工作
				Thread.sleep(1000);
				singletonLazy = new SingletonLazy2();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return singletonLazy;
	}
}
// 单例模式的懒汉实现3--线程安全
// 通过设置同步代码块,效率也太低,整个代码块被加锁
class SingletonLazy3 {

	private static SingletonLazy3 singletonLazy;

	private SingletonLazy3() {

	}

	public static SingletonLazy3 getInstance() {
		try {
			synchronized (SingletonLazy3.class) {
				if (null == singletonLazy) {
					// 模拟在创建对象之前做一些准备工作
					Thread.sleep(1000);
					singletonLazy = new SingletonLazy3();
				}
			}
		} catch (InterruptedException e) {
			// TODO: handle exception
		}
		return singletonLazy;
	}
}
//单例模式的懒汉实现5--线程安全
//通过设置同步代码块,使用DCL双检查锁机制
//使用双检查锁机制成功的解决了单例模式的懒汉实现的线程不安全问题和效率问题
//DCL 也是大多数多线程结合单例模式使用的解决方案
class SingletonLazy5 {

	private static volatile SingletonLazy5 singletonLazy;

	private SingletonLazy5() {

	}

	public static SingletonLazy5 getInstance() {
		try {
			if (null == singletonLazy) {
				// 模拟在创建对象之前做一些准备工作
				Thread.sleep(1000);
				synchronized (SingletonLazy5.class) {
					if(null == singletonLazy) {
						singletonLazy = new SingletonLazy5();
					}
				}
			}
		} catch (InterruptedException e) {
			// TODO: handle exception
		}
		return singletonLazy;
	}
}

java 饿汉式实现singleton

饿汉式是立即加载,声明并创建按类为静态类,并返回改类,在类加载之前就已经创建class实例。缺点是无论该类使用与否都会占用资源,如果类本身占用的资源很大就不适合,适合于占用资源少的class
class SingletonHungary {
	private static SingletonHungary singletonHungary = new SingletonHungary();
	//将构造器设置为private禁止通过new进行实例化
	private SingletonHungary() {
		
	}
	public static SingletonHungary getInstance() {
		return singletonHungary;
	}
}

原文参照:
链接:饿汉式-懒汉式
链接:enum实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单例模式是一种常见的设计模式,用于确保一个类只有一个实例,并提供全局访问点。在 Java 中,实现单例模式的方法有多种,其中最常见的是懒汉式和饿汉式。 1. 懒汉式 懒汉式是指在第一次需要使用实例时才创建它。这种方式需要使用一个私有静态变量来保存实例,并提供一个公有静态方法来获取它。具体实现如下: ``` public class Singleton { private static Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } ``` 在这个实现中,私有变量 `instance` 用于保存实例。在 `getInstance()` 方法中,如果 `instance` 为 `null`,则创建一个新的实例并返回,否则直接返回现有的实例。该实现的缺点是在多线程环境下可能会有竞争条件,需要进行额外的同步处理。 2. 饿汉式 饿汉式是指在类加载时就创建实例。这种方式需要使用一个私有静态变量来保存实例,并在静态初始化代码块中创建它。具体实现如下: ``` public class Singleton { private static Singleton instance = new Singleton(); private Singleton() {} public static Singleton getInstance() { return instance; } } ``` 在这个实现中,私有变量 `instance` 在类加载时就被创建。由于在类加载时就创建了实例,因此不存在多线程竞争的问题。该实现的缺点是可能会导致应用启动时的性能问题,因为实例的创建可能很耗时。 以上就是 Java 中实现单例模式的两种方式。选择哪种方式取决于具体应用场景和性能要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值