单例设计模式

本文介绍了单例模式的八种常见实现方式,包括饿汉式、懒汉式(线程安全与不安全)、静态内部类和枚举,对比了它们的内存消耗与线程安全性,适合理解不同场景下的设计选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、概念

保证一个类仅有一个实例,并提供一个访问它的全局访问点。

2、八种单例模式

  • 饿汉式(静态常量)
  • 饿汉式(静态代码块)
  • 懒汉式(线程不安全)
  • 懒汉式(线程安全,同步方法)
  • 懒汉式(线程不安全,同步代码块)
  • 懒汉式(双重检查)
  • 静态内部类
  • 枚举
  • 推荐使用饿汉式两种方式、懒汉式(双重检查)、静态内部类和枚举。
  • 饿汉式有浪费内存的缺点。

3、代码

package com.mine.design.singleton;

public class SingletonTest {

	public static void main(String[] args) throws Exception {
		int num = 10;
		// 饿汉式(静态常量)
		System.out.println("=====饿汉式(静态常量)=====");
		for (int i = 0; i < num; i++) {
			new Thread(new Runnable() {
				public void run() {
					Singleton01 instance = Singleton01.getInstance();
					System.out.println(instance.hashCode());
				}
			}).start();
		}
		Thread.sleep(1000);

		// 饿汉式(静态代码块)
		System.out.println("=====饿汉式(静态代码块)=====");
		for (int i = 0; i < num; i++) {
			new Thread(new Runnable() {
				public void run() {
					Singleton02 instance = Singleton02.getInstance();
					System.out.println(instance.hashCode());
				}
			}).start();
		}
		Thread.sleep(1000);

		// 懒汉式(线程不安全)
		System.out.println("=====懒汉式(线程不安全)=====");
		for (int i = 0; i < num; i++) {
			new Thread(new Runnable() {
				public void run() {
					Singleton03 instance = Singleton03.getInstance();
					System.out.println(instance.hashCode());
				}
			}).start();
		}
		Thread.sleep(1000);

		// 懒汉式(线程安全,同步方法)
		System.out.println("=====懒汉式(线程安全,同步方法)=====");
		for (int i = 0; i < num; i++) {
			new Thread(new Runnable() {
				public void run() {
					Singleton04 instance = Singleton04.getInstance();
					System.out.println(instance.hashCode());
				}
			}).start();
		}
		Thread.sleep(1000);

		// 懒汉式(线程不安全,同步代码块)
		System.out.println("=====懒汉式(线程不安全,同步代码块)=====");
		for (int i = 0; i < num; i++) {
			new Thread(new Runnable() {
				public void run() {
					Singleton05 instance = Singleton05.getInstance();
					System.out.println(instance.hashCode());
				}
			}).start();
		}
		Thread.sleep(1000);

		// 懒汉式(双重检查)
		System.out.println("=====懒汉式(双重检查)=====");
		for (int i = 0; i < num; i++) {
			new Thread(new Runnable() {
				public void run() {
					Singleton06 instance = Singleton06.getInstance();
					System.out.println(instance.hashCode());
				}
			}).start();
		}
		Thread.sleep(1000);

		// 静态内部类
		System.out.println("=====静态内部类=====");
		for (int i = 0; i < num; i++) {
			new Thread(new Runnable() {
				public void run() {
					Singleton07 instance = Singleton07.getInstance();
					System.out.println(instance.hashCode());
				}
			}).start();
		}
		Thread.sleep(1000);

		// 枚举
		System.out.println("=====枚举=====");
		for (int i = 0; i < num; i++) {
			new Thread(new Runnable() {
				public void run() {
					Singleton08 instance = Singleton08.getInstance();
					System.out.println(instance.hashCode());
				}
			}).start();
		}
		Thread.sleep(1000);
	}
}

// 饿汉式(静态常量)
class Singleton01 {
	private final static Singleton01 instance = new Singleton01();

	private Singleton01() {
	}

	public static Singleton01 getInstance() {
		return instance;
	}
}

// 饿汉式(静态代码块)
class Singleton02 {
	private final static Singleton02 instance;

	static {
		instance = new Singleton02();
	}

	private Singleton02() {
	}

	public static Singleton02 getInstance() {
		return instance;
	}
}

// 懒汉式(线程不安全)
class Singleton03 {
	private static Singleton03 instance;

	private Singleton03() {
	}

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

// 懒汉式(线程安全,同步方法)
class Singleton04 {
	private static Singleton04 instance;

	private Singleton04() {
	}

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

// 懒汉式(线程不安全,同步代码块)
class Singleton05 {
	private static Singleton05 instance;

	private Singleton05() {
	}

	public static Singleton05 getInstance() {
		if (instance == null) {
			synchronized (Singleton05.class) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				instance = new Singleton05();
			}
		}
		return instance;
	}
}

// 懒汉式(双重检查)
class Singleton06 {
//	private static Singleton06 instance;
	private static volatile Singleton06 instance;

	private Singleton06() {
	}

	public static Singleton06 getInstance() {
		if (instance == null) {
			synchronized (Singleton06.class) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (instance == null) {
					instance = new Singleton06();
				}
			}
		}
		return instance;
	}
}

// 静态内部类
class Singleton07 {
	private Singleton07() {
	}

	private static class InnerClass {
		private static Singleton07 instance = new Singleton07();
	}

	public static Singleton07 getInstance() {
		return InnerClass.instance;
	}
}

// 枚举
enum Singleton08 {
	INSTANCE;
	private Singleton08() {
	}
	
	public static Singleton08 getInstance() {
		return INSTANCE;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值