package com.nasuf.pattern.singleton;
/**
* 单例模式
*/
public class Test {
public static void main(String[] args) {
Singleton1 singleton1 = Singleton1.getInstance();
Singleton1 singleton2 = Singleton1.getInstance();
System.out.println(singleton1 == singleton2); // true
System.out.println(singleton1.hashCode()); // 621009875
System.out.println(singleton2.hashCode()); // 621009875
}
}
/**
* 饿汉式一
* 优点:简单,在类加载时完成实例化,避免了线程同步问题
* 缺点:在类加载时完成实例化,没有达到懒加载效果,如果从未使用过这个实例,则会造成内存浪费
*/
class Singleton1 {
private Singleton1() {
}
private final static Singleton1 instance = new Singleton1();
public static Singleton1 getInstance() {
return instance;
}
}
/**
* 饿汉式二
* 优点:简单,在类加载时完成实例化,避免了线程同步问题
* 缺点:在类加载时完成实例化,没有达到懒加载效果,如果从未使用过这个实例,则会造成内存浪费
*/
class Singleton2 {
private Singleton2() {
}
private static Singleton2 instance;
static {
instance = new Singleton2();
}
public static Singleton2 getInstance() {
return instance;
}
}
/**
* 懒汉式一
* 优点:懒加载效果
* 缺点:线程不安全,只能应用在单线程情况下
*/
class Singleton3 {
private Singleton3() {
}
private static Singleton3 instance;
public static Singleton3 getInstance() {
if (instance == null) {
instance = new Singleton3();
}
return instance;
}
}
/**
* 懒汉式二
* 优点:懒加载效果,线程安全
* 缺点:效率低,每个线程在想获得类的实例时,执行getInstance方法都要进行同步
*/
class Singleton4 {
private Singleton4() {
}
private static Singleton4 instance;
public static synchronized Singleton4 getInstance() {
if (instance == null) {
instance = new Singleton4();
}
return instance;
}
}
/**
* 懒汉式三
* 缺点:线程不安全,效率低,每个线程在想获得类的实例时,执行getInstance方法都要进行同步
*/
class Singleton5 {
private Singleton5() {
}
private static Singleton5 instance;
public static Singleton5 getInstance() {
if (instance == null) {
synchronized (Singleton5.class) {
instance = new Singleton5();
}
}
return instance;
}
}
/**
* 双重检查(推荐使用)
*/
class Singleton6 {
private Singleton6() {
}
private static volatile Singleton6 instance;
public static Singleton6 getInstance() {
if (instance == null) {
synchronized (Singleton6.class) {
if (instance == null) {
instance = new Singleton6();
}
}
}
return instance;
}
}
/**
* 静态内部类(推荐使用)
* 外部类装载时,静态内部类不会被装载;内部类只会被装载一次,在类进行初始化时,其他线程无法进入
* 保证了线程安全;
*/
class Singleton7 {
private Singleton7() {
}
private static class SingletonInstance {
private static final Singleton7 INSTANCE = new Singleton7();
}
public static synchronized Singleton7 getInstance() {
return SingletonInstance.INSTANCE;
}
}
/**
* 枚举(推荐使用)
* 线程安全,防止反序列化重新创建新的对象
* 使用:Singleton8 instance = Singleton.INSTANCE
*/
enum Singleton8 {
INSTANCE;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。