设计模式 ~ 创建型模式 ~ 单例模式 ~ Singleton Pattern。
文章目录
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,ta 提供了一种创建对象的最佳方式。
这种模式涉及到了一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问 ,不需要实例化该类的对象。
单例模式的结构。
-
单例类。
只能创建一个实例的类。 -
访问类。
使用单例类。
dlc
。
静态内部类
。
枚举方式
。
单例模式的实现。
饿汉式。(类加载就会创建该类的实例对象)。
懒汉式。(类加载不会创建该类的实例对象,而是首次使用该对象时才会创建)。
饿汉式 ~ 静态成员变量。
package com.geek.singleton.pattern.hungry.staticMemberVariable;
/**
* 单例 ~ 饿汉式 ~ 静态成员变量。
*
* @author geek
*/
public class Singleton {
/**
* 在本类创建本类对象。
*/
private static final Singleton INSTANCE = new Singleton();
/**
* 私有化构造方法。
*/
private Singleton() {
}
/**
* 提供一个公共的访问方式,让外界获取该对象。
*
* @return
*/
public static Singleton getInstance() {
return INSTANCE;
}
}
package com.geek.singleton.pattern.hungry.staticMemberVariable;
/**
* @author geek
*/
public class Client {
public static void main(String[] args) {
// 创建 Singleton 类的对象。
Singleton instance = Singleton.getInstance();
System.out.println("instance = " + instance);
Singleton instance1 = Singleton.getInstance();
System.out.println("instance1 = " + instance1);
// 判断两个对象是否是同一个对象。
System.out.println(instance == instance1);
// true
}
}
该方式在成员变量位置声明 Singleton 类型的静态变量,并创建 Singleton 类的对象 instance。instance 对象是随着类加载而创建的。如果该对象足够大的话,而一直没有使用就会在成内存的浪费。
饿汉式 ~ 静态代码块。
package com.geek.singleton.pattern.hungry.staticBlock;
/**
* 单例 ~ 饿汉式 ~ 静态代码块。
*
* @author geek
*/
public class Singleton {
/**
* 声明 Singleton 类型的变量。
* // null。
*/
private static final Singleton SINGLETON;
// 在静态代码块中进行赋值。
static {
SINGLETON = new Singleton();
}
/**
* 私有化构造方法。
*/
private Singleton() {
}
/**
* 提供一个公共的访问方式,让外界获取该对象。
*
* @return
*/
public static Singleton getSingleton() {
return SINGLETON;
}
}
package com.geek.singleton.pattern.hungry.staticBlock;
/**
* @author geek
*/
public class Client {
public static void main(String[] args) {
// 创建 Singleton 类的对象。
Singleton instance = Singleton.getSingleton();
System.out.println("instance = " + instance);
Singleton instance1 = Singleton.getSingleton();
System.out.println("instance1 = " + instance1);
// 判断两个对象是否是同一个对象。
System.out.println(instance == instance1);
}
}
该方式在成员变量位置声明 Singleton 类型的静态变量,而对象的场景是在静态代码块中,也是对着类加载而创建。所以和饿汉式的方式 1 基本一样,该方式也存在内存浪费的问题。
懒汉式。(类加载不会创建该类的实例对象,而是首次使用该对象时才会创建)。
package com.geek.singleton.pattern.lazy.synchronize;
/**
* 单例模式 ~ 懒汉式。
*
* @author geek
*/
public class Singleton {
/**
* 声明 Singleton 类型的变量。
* // null。
* 只是声明一个该类的对象,并没有进行赋值。
*/
private static Singleton instance;
/**
* 私有化构造方法。
*/
private Singleton() {
}
/**
* 提供一个公共的访问方式,让外界获取该对象。
*
* @return
*/
public static synchronized Singleton getInstance() {
// 判断 instance 是否为 null,如果为 null,说明还没有创建 Singleton 类的对象。
// 如果没有,创建一个并返回。如果有,直接返回。
if (instance == null) {
// 如果此时线程 1 等待,线程 2 获取到 cpu 的执行权,也会进入到该判断中。
// ~ 加 synchronized。
instance = new Singleton();
}
return instance;
}
}
懒汉式 ~ 双重检查锁。
懒汉模式加锁的问题。对于 getInstance(); 方法来说,绝大部分的操作都是读操作,读操作是线程安全的,所以没有必要让每个线程必须持有锁才能调用该方法,我们需要调整加锁的时机。由此产生了一种新的实现模式:双重检查锁模式。
package com.geek.singleton.pattern.lazy.dcl;
/**
* 单例模式 ~ 懒汉式 ~ 双重检查锁模式。
*
* @author geek
*/
public class Singleton {
/**
* 声明 Singleton 类型的变量。(懒汉式)。
* // null。
* 只是声明一个该类的对象,并没有进行赋值。
* // volatile
* 双重检查锁模式是一种非常好的单例实现模式,解决了单例、性能、线程安全问题。上面的双重检查锁模式看上去完美无缺,其实是存在问题的。在多线程的情况下,可能会出现空指针的问题,出现问题的原因是 JVM 在实例化对象的时候会进行优化和指令重排序操作。要解决双重检查锁模式带来的 NNP 问题,只需要使用 `volatile` 关键字,`volatile` 关键字可以保证可见性和有序性。
*/
private static volatile Singleton instance;
/**
* 私有化构造方法。
*/
private Singleton() {
}
/**
* 提供一个公共的访问方式,让外界获取该对象。
*
* @return
*/
public static Singleton getInstance() {
// 第一次判断。如果不为 null,不需要抢占锁,直接返回。
if (instance == null) {
// 如果此时线程 1 等待,线程 2 获取到 cpu 的执行权,也会进入到该判断中。
// ∴ synchronized。
synchronized (Singleton.class) {
// 第二次判断。
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
双重检查锁模式是一种非常好的单例实现模式,解决了单例、性能、线程安全问题。上面的双重检查锁模式看上去完美无缺,其实是存在问题的。在多线程的情况下,可能会出现空指针的问题,出现问题的原因是 JVM 在实例化对象的时候会进行优化和指令重排序操作。要解决双重检查锁模式带来的 NNP 问题,只需要使用
volatile
关键字,volatile
关键字可以保证可见性和有序性。
/**
* 声明 Singleton 类型的变量。
* // null。
* 只是声明一个该类的对象,并没有进行赋值。
* // volatile
*/
private static volatile Singleton instance;
懒汉式 ~ 静态内部类。
静态内部类单例模式中实例由内部类创建。由于 JVM 在加载外部类的过程中,是不会加载静态内部类的,只有在内部类的属性/方法被调用时才会被加载,并初始化其静态属性。静态属性由于被 static
修饰,保证只被实例化一次,并且严格保证实例化顺序。
package com.geek.singleton.pattern.lazy.staticInnerClass;
/**
* 单例模式 ~ 懒汉式 ~ 静态内部类。
* 静态内部类单例模式中实例由内部类创建。由于 JVM 在加载外部类的过程中,是不会加载静态内部类的,只有在内部类的属性/方法被调用时才会被加载,并初始化其静态属性。静态属性由于被 `static` 修饰,保证只被实例化一次,并且严格保证实例化顺序。
* <p>
* 第一次加载 Singleton 类时不会去初始化 INSTANCE,只有第一次调用 getInstance(); 虚拟机加载 SingletonHolder 并初始化 INSTANCE,这样不仅能确保线程安全,也能保证 Singleton 类的唯一性。
* 静态内部类单例模式是一种优秀的单例模式,是开源项目中比较常用的一种单例模式。在没有任何锁的情况下,保证了多线程下的安全,并且没有任何性能影响和空间的浪费。
*
* @author geek
*/
public class Singleton {
/**
* 私有构造方法。
*/
private Singleton() {
}
/**
* 提供一个公共的访问方式,让外界获取该对象。
*
* @return
*/
public static synchronized Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
/**
* 定义一个静态内部类。
*/
private static class SingletonHolder {
// 在内部类中声明并初始化外部类的对象。
private static final Singleton INSTANCE = new Singleton();
}
}
枚举方式。
枚举方式实现的单例模式是极力推荐的单例模式实现。
枚举类型是线程安全的,并且只会装载一次,设计者充分的利用了枚举的这个特性来实现单例模式。枚举的写法非常简单,而且枚举类型是所有单例实现中唯一不会被破坏的单例模式实现。
枚举方式属于饿汉式方式。(浪费内存空间)。
package com.geek.singleton.pattern.hungry.enumSingleton;
/**
* 单例 ~ 枚举 ~ 饿汉式。
*
* @author geek
*/
public enum Singleton {
INSTANCE
}
package com.geek.singleton.pattern.hungry.enumSingleton;
/**
* @author geek
*/
public class Client {
public static void main(String[] args) {
Singleton instance = Singleton.INSTANCE;
Singleton instance1 = Singleton.INSTANCE;
System.out.println("instance == instance1 = " + (instance == instance1));
// true
}
}
饿汉式 ~ 静态成员变量。
该方式在成员位置声明 Singleton 类型的静态变量,并创建 Singleton 类的对象 instance。
instance 对象是随着类的加载而创建的。如果该对象足够大的话,而一直没有使用就会造成内存的浪费。
package com.geek.singleton.hungry1;
/**
* 饿汉式单例模式。
* 静态成员变量。
*
* @author geek
*/
public class Hungry {
// 饿汉,类一加载就创建对象。
private final static Hungry HUNGRY = new Hungry();
// 可能浪费空间。
private byte[] data1 = new byte[1024 * 1024];
private byte[] data2 = new byte[1024 * 1024];
private byte[] data3 = new byte[1024 * 1024];
private byte[] data4 = new byte[1024 * 1024];
/**
* 构造器私有。
*/
private Hungry() {
}
/**
* 构造器私有了,外界无法构造创建,给外界提供一个获取对象的方法。
*
* @return
*/
public static Hungry getInstance() {
return HUNGRY;
}
}
package com.geek.singleton.hungry1;
/**
* @author geek
*/
public class HungryTest {
public static void main(String[] args) {
Hungry instance = Hungry.getInstance();
Hungry instance1 = Hungry.getInstance();
System.out.println(instance == instance1);
// true
}
}
饿汉式 ~ 静态代码块。
该方式在成员位置声明 Singleton 类型的静态变量,而对象的创建是在静态代码块中,也是随着类的加载而创建。所以和饿汉式的方式 1 基本上一样,当然该方式也存在内存浪费问题。
package com.geek.singleton.pattern.hungry.staticBlock;
/**
* 单例 ~ 饿汉式 ~ 静态代码块。
*
* @author geek
*/
public class Singleton {
/**
* 声明 Singleton 类型的变量。
* // null。
*/
private static Singleton instance;
// 在静态代码块中进行赋值。
static {
instance = new Singleton();
}
/**
* 私有化构造方法。
*/
private Singleton() {
}
/**
* 提供一个公共的访问方式,让外界获取该对象。
*
* @return
*/
public static Singleton getInstance() {
return instance;
}
}
package com.geek.singleton.hungry2;
/**
* @author geek
*/
public class HungryTest {
public static void main(String[] args) {
Hungry instance = Hungry.getInstance();
Hungry instance1 = Hungry.getInstance();
System.out.println(instance == instance1);
// true
}
}
懒汉式。
该方式在成员位置声明 Singleton 类型的静态变量,并没有进行对象的赋值操作,那么什么时候赋值的呢?当调用 getInstance(); 方法获取 Singleton 类的对象的时候才创建 Singleton 类的对象,这样就实现了懒加载的效果。但是,如果是多线程环境,会出现线程安全问题。
- 单线程 ok。
多线程失效。
package com.geek.singleton.lazy;
/**
* 懒汉式单例模式。
* 首次使用该对象时创建。
*
* @author geek
*/
public class Lazy {
private static Lazy lazy;
/**
* 私有构造方法。
*/
private Lazy() {
System.out.println(Thread.currentThread().getName() + " ok.");
}
/**
* 构造器私有了,外界无法构造创建,给外界提供一个获取对象的方法。
*
* @return
*/
// public static Lazy getInstance() {
public static synchronized Lazy getInstance() {
// 判断 instance 是否为 null,如果为 null,说明还没有创建 Lazy 类的对象。
// 如果没有,创建一个并返回。如果有,直接返回。
if (lazy == null) {
lazy = new Lazy();
}
return lazy;
}
// 单线程下单例 ok。
// 多线程并发。
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(Lazy::getInstance).start();
}
}
// Thread-0 ok.
//Thread-5 ok.
//Thread-4 ok.
//Thread-2 ok.
//Thread-1 ok.
//Thread-3 ok.
//
//Process finished with exit code 0
}
package com.geek.singleton.lazy;
/**
* @author geek
*/
public class LazyTest {
public static void main(String[] args) {
Lazy instance = Lazy.getInstance();
Lazy instance1 = Lazy.getInstance();
System.out.println(instance == instance1);
// true
}
}
- 解决:给 getInstance(); 方法加 synchronized。
该方式也实现了懒加载效果,同时又解决了线程安全问题。但是在 getInstance(); 方法上添加了 synchronized 关键字,导致该方法的执行效果特别低。从上面代码我们可以看出,其实就是在初始化 instance 的时候才会出现线程安全问题,一旦初始化完成就不存在了。
懒汉式 ~ 改进:双重检测锁懒汉式单例。DCL Double Check Lock 懒汉式单例。
再来讨论一下懒汉模式中加锁的问题,对于 getInstance(); 方法来说,绝大部分的操作都是读操作,读操作是线程安全的,所以我们没必让每个线程必须持有锁才能调用该方法,我们需要调整加锁的时机。由此也产生了一种新的实现模式:双重检查锁模式。
package com.geek.singleton.lazy.dcl;
/**
* 懒汉单例 ~ 双重检查锁。
*
* @author geek
*/
public class Singleton {
/**
* 声明 Singleton 类型的变量。
*/
private static Singleton instance;
/**
* 私有构造方法。
*/
private Singleton() {
System.out.println(Thread.currentThread().getName() + " ok.");
}
/**
* 对外提供访问方式。
*
* @return
*/
public static Singleton getInstance() {
// 第一次判断。如果 instance 不为 null,不需要抢占,直接返回对象。
if (instance == null) {
synchronized (Singleton.class) {
// 第二次判断。
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
加 volatile 是因为 new 对象不是原子性操作。
package com.geek.singleton.lazy.dcl;
/**
* @author geek
*/
public class DCLTest {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance == instance1);
// true
}
}
双重检查锁模式是一种非常好的单例实现模式,解决了单例、性能、线程安全问题,上面的双重检测锁模式看上去完美无缺,其实是存在问题,在多线程的情况下,可能会出现空指针问题,出现问题的原因是 JVM 在实例化对象的时候会进行优化和指令重排序操作。
要解决双重检查锁模式带来空指针异常的问题,只需要使用 volatile
关键字,volatile
关键字可以保证可见性和有序性。
破坏单例模式。
使上面定义的单例类(Singleton)可以创建多个对象,枚举方式除外。有两种方式,分别是序列化
和反射
。
序列化反序列化。
package com.geek.singleton.pattern.destroy;
import java.io.Serializable;
/**
* 静态内部类实现懒汉。
* 静态内部类单例模式中实例由内部类创建,由于 JVM 在加载外部类的过程中,是不会加载静态内部类的,只有内部类的属性/方法被调用时才会被加载,并初始化其静态属性。静态属性由于被 static 修饰,保证只被实例化一次,并且严格保证实例化顺序。
*
* @author geek
*/
public class Singleton implements Serializable {
// java.io.NotSerializableException
// 第一次加载 Singleton 类时不会去初始化 INSTANCE,只有第一次调用 getInstance();,虚拟机加载 SingletonHolder 并初始化 INSTANCE,这样不仅能确保线程安全,也能保证 Singleton 类的唯一性。
// 静态内部类单例模式是一种优秀的单例模式,是开源项目中比较常用的一种单例模式。在没有加任何锁的情况下,保证了多线程下的安全,并且没有任何性能影响和空间的浪费。
/**
* 私有构造方法。
*/
private Singleton() {
}
/**
* 提供公共访问方式。
*
* @return
*/
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
/**
* 定义一个静态内部类。
* private ~ 防止外界访问。
*/
private static class SingletonHolder {
// 在内部类中声明并初始化外部类对象。
private static final Singleton INSTANCE = new Singleton();
}
}
package com.geek.singleton.pattern.destroy;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* 序列化反序列化破坏单例 ~ 静态内部类。
*
* @author geek
*/
public class NoIOTest {
public static void main(String[] args) throws Exception {
// 往文件中写对象。
writeObject2File();
// 从文件中读取对象。
Singleton s1 = readObjectFromFile();
Singleton s2 = readObjectFromFile();
// 判断两个反序列化后的对象是否是同一个对象。
System.out.println(s1 == s2);
// instance = Singleton@6d03e736
//instance = Singleton@568db2f2
//false
}
/**
* 从文件读取对象。
*
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
private static Singleton readObjectFromFile() throws IOException, ClassNotFoundException {
// 创建对象输入流对象。
ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(Paths.get("object.txt")));
// 读取 Singleton 对象。
Singleton instance = (Singleton) objectInputStream.readObject();
System.out.println("instance = " + instance);
objectInputStream.close();
return instance;
}
/**
* 向文件中写数据(对象)。
*
* @throws IOException
*/
public static void writeObject2File() throws IOException {
// 获取 Singleton 类的对象。
Singleton instance = Singleton.getInstance();
// 创建对象输出流。
// ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("./object.txt"));
ObjectOutputStream objectOutputStream = new ObjectOutputStream(Files.newOutputStream(Paths.get("./object.txt")));
// 将 instance 对象写出到文件中。
objectOutputStream.writeObject(instance);
// 释放资源。
objectOutputStream.close();
}
}
反射破坏。
package com.geek.singleton.pattern.destroy;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* 反射破坏单例 ~ 静态内部类。
*
* @author geek
*/
public class NoReflectTest {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
// 获取 Singleton 的字节码对象。
Class<Singleton> singletonClass = Singleton.class;
// 获取无参构造方法对象。
Constructor<Singleton> declaredConstructor = singletonClass.getDeclaredConstructor();
// 取消访问检查。
declaredConstructor.setAccessible(true);
/*
Exception in thread "main" java.lang.IllegalAccessException: Class com.geek.singleton.pattern.destroy.NoReflectTest can not access a member of class com.geek.singleton.pattern.destroy.Singleton with modifiers "private"
*/
// 创建 Singleton 对象。
Singleton singleton = declaredConstructor.newInstance();
Singleton singleton2 = declaredConstructor.newInstance();
System.out.println(singleton);
System.out.println(singleton2);
// com.geek.singleton.pattern.destroy.Singleton@8efb846
// com.geek.singleton.pattern.destroy.Singleton@2a84aee7
}
}
解决。
- 序列化、反序列方式破坏单例模式的解决方法。
在 Singleton 类中添加 readResolve();
方法,在反序列化时被反射调用,如果定义了这个方法,就返回这个方法的值,如果没有定义,则返回新 new 出来的对象。
Singleton instance = (Singleton) objectInputStream.readObject();
源码。
/**
* Read an object from the ObjectInputStream. The class of the object, the
* signature of the class, and the values of the non-transient and
* non-static fields of the class and all of its supertypes are read.
* Default deserializing for a class can be overridden using the writeObject
* and readObject methods. Objects referenced by this object are read
* transitively so that a complete equivalent graph of objects is
* reconstructed by readObject.
*
* <p>The root object is completely restored when all of its fields and the
* objects it references are completely restored. At this point the object
* validation callbacks are executed in order based on their registered
* priorities. The callbacks are registered by objects (in the readObject
* special methods) as they are individually restored.
*
* <p>Exceptions are thrown for problems with the InputStream and for
* classes that should not be deserialized. All exceptions are fatal to
* the InputStream and leave it in an indeterminate state; it is up to the
* caller to ignore or recover the stream state.
*
* @throws ClassNotFoundException Class of a serialized object cannot be
* found.
* @throws InvalidClassException Something is wrong with a class used by
* serialization.
* @throws StreamCorruptedException Control information in the
* stream is inconsistent.
* @throws OptionalDataException Primitive data was found in the
* stream instead of objects.
* @throws IOException Any of the usual Input/Output related exceptions.
*/
public final Object readObject()
throws IOException, ClassNotFoundException {
return readObject(Object.class);
}
/**
* Internal method to read an object from the ObjectInputStream of the expected type.
* Called only from {@code readObject()} and {@code readString()}.
* Only {@code Object.class} and {@code String.class} are supported.
*
* @param type the type expected; either Object.class or String.class
* @return an object of the type
* @throws IOException Any of the usual Input/Output related exceptions.
* @throws ClassNotFoundException Class of a serialized object cannot be
* found.
*/
private final Object readObject(Class<?> type)
throws IOException, ClassNotFoundException
{
if (enableOverride) {
return readObjectOverride();
}
if (! (type == Object.class || type == String.class))
throw new AssertionError("internal error");
// if nested read, passHandle contains handle of enclosing object
int outerHandle = passHandle;
try {
// 重点查看 readObject0(); 方法。
Object obj = readObject0(type, false);
handles.markDependency(outerHandle, passHandle);
ClassNotFoundException ex = handles.lookupException(passHandle);
if (ex != null) {
throw ex;
}
if (depth == 0) {
vlist.doCallbacks();
}
return obj;
} finally {
passHandle = outerHandle;
if (closed && depth == 0) {
clear();
}
}
}
/**
* Underlying readObject implementation.
* @param type a type expected to be deserialized; non-null
* @param unshared true if the object can not be a reference to a shared object, otherwise false
*/
private Object readObject0(Class<?> type, boolean unshared) throws IOException {
boolean oldMode = bin.getBlockDataMode();
if (oldMode) {
int remain = bin.currentBlockRemaining();
if (remain > 0) {
throw new OptionalDataException(remain);
} else if (defaultDataEnd) {
/*
* Fix for 4360508: stream is currently at the end of a field
* value block written via default serialization; since there
* is no terminating TC_ENDBLOCKDATA tag, simulate
* end-of-custom-data behavior explicitly.
*/
throw new OptionalDataException(true);
}
bin.setBlockDataMode(false);
}
byte tc;
while ((tc = bin.peekByte()) == TC_RESET) {
bin.readByte();
handleReset();
}
depth++;
totalObjectRefs++;
try {
switch (tc) {
case TC_NULL:
return readNull();
case TC_REFERENCE:
// check the type of the existing object
return type.cast(readHandle(unshared));
case TC_CLASS:
if (type == String.class) {
throw new ClassCastException("Cannot cast a class to java.lang.String");
}
return readClass(unshared);
case TC_CLASSDESC:
case TC_PROXYCLASSDESC:
if (type == String.class) {
throw new ClassCastException("Cannot cast a class to java.lang.String");
}
return readClassDesc(unshared);
case TC_STRING:
case TC_LONGSTRING:
return checkResolve(readString(unshared));
case TC_ARRAY:
if (type == String.class) {
throw new ClassCastException("Cannot cast an array to java.lang.String");
}
return checkResolve(readArray(unshared));
case TC_ENUM:
if (type == String.class) {
throw new ClassCastException("Cannot cast an enum to java.lang.String");
}
return checkResolve(readEnum(unshared));
case TC_OBJECT:
if (type == String.class) {
throw new ClassCastException("Cannot cast an object to java.lang.String");
}
// 重点查看 readOrdinaryObject(); 方法。
return checkResolve(readOrdinaryObject(unshared));
case TC_EXCEPTION:
if (type == String.class) {
throw new ClassCastException("Cannot cast an exception to java.lang.String");
}
IOException ex = readFatalException();
throw new WriteAbortedException("writing aborted", ex);
case TC_BLOCKDATA:
case TC_BLOCKDATALONG:
if (oldMode) {
bin.setBlockDataMode(true);
bin.peek(); // force header read
throw new OptionalDataException(
bin.currentBlockRemaining());
} else {
throw new StreamCorruptedException(
"unexpected block data");
}
case TC_ENDBLOCKDATA:
if (oldMode) {
throw new OptionalDataException(true);
} else {
throw new StreamCorruptedException(
"unexpected end of block data");
}
default:
throw new StreamCorruptedException(
String.format("invalid type code: %02X", tc));
}
} finally {
depth--;
bin.setBlockDataMode(oldMode);
}
}
/**
* Reads and returns "ordinary" (i.e., not a String, Class,
* ObjectStreamClass, array, or enum constant) object, or null if object's
* class is unresolvable (in which case a ClassNotFoundException will be
* associated with object's handle). Sets passHandle to object's assigned
* handle.
*/
private Object readOrdinaryObject(boolean unshared)
throws IOException
{
if (bin.readByte() != TC_OBJECT) {
throw new InternalError();
}
ObjectStreamClass desc = readClassDesc(false);
desc.checkDeserialize();
Class<?> cl = desc.forClass();
if (cl == String.class || cl == Class.class
|| cl == ObjectStreamClass.class) {
throw new InvalidClassException("invalid class descriptor");
}
Object obj;
try {
// isInstantiable 返回 true,执行 desc.newInstance(),通过反射创建新的单例类。
obj = desc.isInstantiable() ? desc.newInstance() : null;
} catch (Exception ex) {
throw (IOException) new InvalidClassException(
desc.forClass().getName(),
"unable to create instance").initCause(ex);
}
passHandle = handles.assign(unshared ? unsharedMarker : obj);
ClassNotFoundException resolveEx = desc.getResolveException();
if (resolveEx != null) {
handles.markException(passHandle, resolveEx);
}
if (desc.isExternalizable()) {
readExternalData((Externalizable) obj, desc);
} else {
readSerialData(obj, desc);
}
handles.finish(passHandle);
// 在 Singleton 类中添加 readResolve(); 方法后 desc.hasReadResolveMethod(); 方法执行结果为 true。
if (obj != null &&
handles.lookupException(passHandle) == null &&
desc.hasReadResolveMethod())
{
// 通过反射调用 Singleton 类中的 readResolve(); 方法,将返回值赋值给 rep 变量
// 这样多次调用 ObjectInputStream 类中的 readObject(); 方法,继而就会调用我们定义的 readResolve(); 方法,所以返回的是同一个对象。
Object rep = desc.invokeReadResolve(obj);
if (unshared && rep.getClass().isArray()) {
rep = cloneArray(rep);
}
if (rep != obj) {
// Filter the replacement object
if (rep != null) {
if (rep.getClass().isArray()) {
filterCheck(rep.getClass(), Array.getLength(rep));
} else {
filterCheck(rep.getClass(), -1);
}
}
handles.setObject(passHandle, obj = rep);
}
}
return obj;
}
- 实现。
package com.geek.singleton.pattern.destroy.resolve;
import java.io.Serializable;
/**
* 单例模式 ~ 静态内部类。
* 静态内部类单例模式中实例由内部类创建。由于 JVM 在加载外部类的过程中,是不会加载静态内部类的,只有在内部类的属性/方法被调用时才会被加载,并初始化其静态属性。静态属性由于被 `static` 修饰,保证只被实例化一次,并且严格保证实例化顺序。
* <p>
* 第一次加载 Singleton 类时不会去初始化 INSTANCE,只有第一次调用 getInstance(); 虚拟机加载 SingletonHolder 并初始化 INSTANCE,这样不仅能确保线程安全,也能保证 Singleton 类的唯一性。
* 静态内部类单例模式是一种优秀的单例模式,是开源项目中比较常用的一种单例模式。在没有任何锁的情况下,保证了多线程下的安全,并且没有任何性能影响和空间的浪费。
*
* @author geek
*/
public class Singleton implements Serializable {
/**
* 私有构造方法。
*/
private Singleton() {
}
/**
* 提供一个公共的访问方式,让外界获取该对象。
*
* @return
*/
public static synchronized Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
/**
* 当进行反序列化时,会自动调用该方法,将方法的返回值直接返回。
*
* @return
*/
public Object readResolve() {
return SingletonHolder.INSTANCE;
}
/**
* 定义一个静态内部类。
*/
private static class SingletonHolder {
// 在内部类中声明并初始化外部类的对象。
private static final Singleton INSTANCE = new Singleton();
}
}
package com.geek.singleton.no.yes.io;
import java.io.*;
/**
* 解决序列化反序列化破坏单例 ~ 静态内部类。
*
* @author geek
*/
public class IOTest {
public static void main(String[] args) throws Exception {
// 往文件中写对象。
// writeObject2File();
// 从文件中读取对象。
Singleton s1 = readObjectFromFile();
Singleton s2 = readObjectFromFile();
// 判断两个反序列化后的对象是否是同一个对象。
System.out.println(s1 == s2);
// instance = com.geek.singleton.no.yes.io.Singleton@6d03e736
//instance = com.geek.singleton.no.yes.io.Singleton@6d03e736
//true
}
/**
* 从文件读取对象。
*
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
private static Singleton readObjectFromFile() throws IOException, ClassNotFoundException {
// 创建对象输入流对象。
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("object.txt"));
// 读取 Singleton 对象。
Singleton instance = (Singleton) objectInputStream.readObject();
System.out.println("instance = " + instance);
objectInputStream.close();
return instance;
}
/**
* 向文件中写数据(对象)。
*
* @throws IOException
*/
public static void writeObject2File() throws IOException {
// 获取 Singleton 类的对象。
Singleton instance = Singleton.getInstance();
// 创建对象输出流。
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
new FileOutputStream("./object.txt"));
// 将 instance 对象写出到文件中。
objectOutputStream.writeObject(instance);
objectOutputStream.close();
}
}
反射方式破解单例解决。
package com.geek.singleton.no.yes.reflect;
import java.io.Serializable;
/**
* 静态内部类实现懒汉。
* 静态内部类单例模式中实例由内部类创建,由于 JVM 在加载外部类的过程中,是不会加载静态内部类的,只有内部类的属性/方法被调用时才会被加载,并初始化其静态属性。静态属性由于被 static 修饰,保证只被实例化一次,并且严格保证实例化顺序。
*
* @author geek
*/
public class Singleton implements Serializable {
// 第一次加载 Singleton 类时不会去初始化 INSTANCE,只有第一次调用 getInstance();,虚拟机加载 SingletonHolder 并初始化 INSTANCE,这样不仅能确保线程安全,也能保证 Singleton 类的唯一性。
// 静态内部类单例模式是一种优秀的单例模式,是开源项目中比较常用的一种单例模式。在没有加任何锁的情况下,保证了多线程下的安全,并且没有任何性能影响和空间的浪费。
/**
* 私有构造方法。
*/
private Singleton() {
}
/**
* 提供公共访问方式。
*
* @return
*/
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
/**
* 当进行反序列化时会自动调用该方法,将该方法的返回值直接返回。
*
* @return
*/
public Object readResolve() {
return SingletonHolder.INSTANCE;
}
/**
* 定义一个静态内部类。
* private ~ 防止外界访问。
*/
private static class SingletonHolder {
// 在内部类中声明并初始化外部类对象。
private static final Singleton INSTANCE = new Singleton();
}
}
package com.geek.singleton.no.yes.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class ReflectTest {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
// 反射破坏单例。
// 获取 Singleton 的字节码对象。
Class<Singletonstatic1.inner.class1> singletonClass = Singletonstatic1.inner.class1.class;
// 获取无参构造方法对象。
Constructor<Singletonstatic1.inner.class1> declaredConstructor = singletonClass.getDeclaredConstructor();
// 取消访问检查。
declaredConstructor.setAccessible(true);
// 创建 Singleton 对象。
Singletonstatic1.inner.class1 instance = declaredConstructor.newInstance();
Singletonstatic1.inner.class1 instance2 = declaredConstructor.newInstance();
System.out.println(instance.hashCode());
System.out.println(instance2.hashCode());
/*
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.geek.singleton.no.yes.reflect.ReflectTest.main(ReflectTest.java:18)
Caused by: java.lang.RuntimeException: 不要试图用反射破坏单例。
at com.geek.singleton.no.yes.reflect.Singletonstatic1.inner.class1.<init>(Singletonstatic1.inner.class1.java:27)
... 5 more
Process finished with exit code 1
*/
}
}
package com.geek.singleton;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* 懒汉式单例模式。
*/
public class LazySynchronized {
private static volatile LazySynchronized lazy;
private LazySynchronized() {
synchronized (LazySynchronized.class) {
if (lazy != null) {
throw new RuntimeException("不要试图用反射破坏单例。");
}
}
System.out.println(Thread.currentThread().getName() + " ok.");
}
// 双重检测锁懒汉式单例。DCL Double Check Lock 懒汉式单例。
public static LazySynchronized getInstance() {
if (lazy == null) {
synchronized (LazySynchronized.class) {
if (lazy == null) {
lazy = new LazySynchronized();// 不是一个原子性操作。
/**
* 1. 分配内存空间。
* 2. 执行构造方法,初始化对象。
* 3. 把引用指向对象。
*
* 期望 1 2 3
* 然而可能 1 3 2
* A 线程先分配内存空间,引用指向了对象,
* 这时 B 线程进来,会认为这个引用不为 null
* 直接 return lazy;。
* 此时 lazy 还没有完成构造,return null。
* (指令重排造成问题。)
*
* 所以要在 lazy 加上 volatile。
*/
}
}
}
System.out.println("lazy = " + lazy);
return lazy;
}
// 单线程下单例 ok。
// 多线程并发。
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
for (int i = 0; i < 10; i++) {
new Thread(LazySynchronized::getInstance).start();
// Thread-0 ok.
//lazy = com.geek.singleton.LazySynchronized@674827d5
//lazy = com.geek.singleton.LazySynchronized@674827d5
//lazy = com.geek.singleton.LazySynchronized@674827d5
//lazy = com.geek.singleton.LazySynchronized@674827d5
//lazy = com.geek.singleton.LazySynchronized@674827d5
//lazy = com.geek.singleton.LazySynchronized@674827d5
//lazy = com.geek.singleton.LazySynchronized@674827d5
//lazy = com.geek.singleton.LazySynchronized@674827d5
//lazy = com.geek.singleton.LazySynchronized@674827d5
//lazy = com.geek.singleton.LazySynchronized@674827d5
//
//Process finished with exit code 0
}
LazySynchronized lazy1 = LazySynchronized.getInstance();
// 反射破坏单例。
Constructor<LazySynchronized> declaredConstructor = LazySynchronized.class.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true);
LazySynchronized lazy2 = declaredConstructor.newInstance();
System.out.println(lazy1);
System.out.println(lazy2);
// Exception in thread "main" lazy = com.geek.singleton.LazySynchronized@6223c513
//java.lang.reflect.InvocationTargetException
// at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
// at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
// at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
// at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
// at com.geek.singleton.LazySynchronized.main(LazySynchronized.java:79)
//Caused by: java.lang.RuntimeException: 不要试图用反射破坏单例。
// at com.geek.singleton.LazySynchronized.<init>(LazySynchronized.java:18)
// ... 5 more
// 解决:
// 在构造方法中加 synchronized 锁。
}
}
package com.geek.singleton;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
/**
* 懒汉式单例模式。
*/
public class LazySynchronized2 {
private static volatile LazySynchronized2 lazy;
private static boolean geek = false;// 使用加密。
private LazySynchronized2() {
synchronized (LazySynchronized2.class) {
// if (lazy != null) {
// throw new RuntimeException("不要试图用反射破坏单例。");
// }
// 方法 2。加密。
if (!geek) {
geek = true;
} else {
throw new RuntimeException("不要试图用反射破坏单例。");
}
}
System.out.println(Thread.currentThread().getName() + " ok.");
}
// 双重检测锁懒汉式单例。DCL Double Check Lock 懒汉式单例。
public static LazySynchronized2 getInstance() {
if (lazy == null) {
synchronized (LazySynchronized2.class) {
if (lazy == null) {
lazy = new LazySynchronized2();// 不是一个原子性操作。
/**
* 1. 分配内存空间。
* 2. 执行构造方法,初始化对象。
* 3. 把引用指向对象。
*
* 期望 1 2 3
* 然而 1 3 2
* A 线程先分配内存空间,引用指向了对象,
* 这时 B 线程进来,会认为这个引用不为 null
* 直接 return lazy;。
* 此时 lazy 还没有完成构造,return null。
* (指令重排造成问题。)
*
* 所以要在 lazy 加上 volatile。
*/
}
}
}
System.out.println("lazy = " + lazy);
return lazy;
}
// 单线程下单例 ok。
// 多线程并发。
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
for (int i = 0; i < 10; i++) {
new Thread(LazySynchronized2::getInstance).start();
// lazy = LazySynchronized@346827f
//lazy = LazySynchronized@346827f
//lazy = LazySynchronized@346827f
}
LazySynchronized2 lazy1 = LazySynchronized2.getInstance();
// 反射破坏单例。
// ~ ~ ~ 2
Field geek = LazySynchronized2.class.getDeclaredField("geek");
geek.setAccessible(true);
// ~ ~ ~
Constructor<LazySynchronized2> declaredConstructor = LazySynchronized2.class.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true);
// ~ ~ ~
geek.set(lazy, false);
// ~ ~ ~
LazySynchronized2 lazy2 = declaredConstructor.newInstance();
System.out.println(lazy1);
System.out.println(lazy2);
// com.geek.singleton.LazySynchronized2@278e4dc4
//com.geek.singleton.LazySynchronized2@41629346
}
// Thread-0 ok.
}
单例 ~ 静态内部类。
静态内部类单例模式中实例由内部类创建,由于 JVM 在加载外部类的过程中,是不会加载静态内部类的,只有内部类的属性/方法被调用时才会被加载,并初始化其静态属性。静态属性由于被 static 修饰,保证只被实例化一次,并且严格保证实例化顺序。
package com.geek.singleton.static1.inner.class1;
/**
* 静态内部类实现懒汉。
* 静态内部类单例模式中实例由内部类创建,由于 JVM 在加载外部类的过程中,是不会加载静态内部类的,只有内部类的属性/方法被调用时才会被加载,并初始化其静态属性。静态属性由于被 static 修饰,保证只被实例化一次,并且严格保证实例化顺序。
*
* @author geek
*/
public class Singleton {
// 第一次加载 Singleton 类时不会去初始化 INSTANCE,只有第一次调用 getInstance();,虚拟机加载 SingletonHolder 并初始化 INSTANCE,这样不仅能确保线程安全,也能保证 Singleton 类的唯一性。
// 静态内部类单例模式是一种优秀的单例模式,是开源项目中比较常用的一种单例模式。在没有加任何锁的情况下,保证了多线程下的安全,并且没有任何性能影响和空间的浪费。
/**
* 私有构造方法。
*/
private Singleton() {
}
/**
* 提供公共访问方式。
*
* @return
*/
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
/**
* 定义一个静态内部类。
* private ~ 防止外界访问。
*/
private static class SingletonHolder {
// 在内部类中声明并初始化外部类对象。
private static final Singleton INSTANCE = new Singleton();
}
}
package com.geek.singleton.static1.inner.class1;
/**
* @author geek
*/
public class Test {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance.hashCode());
System.out.println(instance1.hashCode());
// 460141958
//460141958
}
}
单例 ~ 枚举。
枚举属于饿汉式。
枚举类实现单例模式是极力推荐的单例实现模式,因为枚举类型是线程安全的,并且只会装载一次,设计者充分的利用了枚举的这个特性来实现单例模式,枚举的写法非常简单,而且枚举类型是所用单例实现中唯一一种不会被破坏的单例实现模式。
package com.geek.singleton.enum.singleton;
/**
* 枚举属于饿汉式。
* enum 本身也是一个类 class。
*
* @author geek
*/
public enum EnumSingle {
/**
* 单例。
*/
INSTANCE;
public EnumSingle getInstance() {
return INSTANCE;
}
}
package com.geek.singleton.enum.singleton;
/**
* @author geek
*/
public class TestEnum {
public static void main(String[] args) {
EnumSingle instance = EnumSingle.INSTANCE;
EnumSingle instance1 = EnumSingle.INSTANCE;
System.out.println(instance.hashCode());
System.out.println(instance1.hashCode());
// 460141958
//460141958
}
}
package com.geek.singleton;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
// enum 本身也是一个类 class。
public enum EnumSingle {
INSTANCE;
public EnumSingle getInstance() {
return INSTANCE;
}
}
class Test {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
EnumSingle instance1 = EnumSingle.INSTANCE;
// Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(null);
// Exception in thread "main" java.lang.NoSuchMethodException: com.geek.singleton.EnumSingle.<init>()
// IDEA 骗了我,EnumSingle 没有空参构造。骗子 +1。
Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class, int.class);
// Exception in thread "main" java.lang.IllegalArgumentException: Cannot reflectively create enum objects
// at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
// 想要的异常。不能反射创建枚举。
declaredConstructor.setAccessible(true);
EnumSingle instance2 = declaredConstructor.newInstance();
System.out.println(instance1);
System.out.println(instance2);
// EnumSingle instance2 = EnumSingle.INSTANCE;
//
// System.out.println("instance1 = " + instance1);// INSTANCE
// System.out.println("instance2 = " + instance2);// INSTANCE
}
}
/*
可以看到,EnumSingle 本身也是 class,只是继承了 Enum 类。
这里还是有空参构造。可是运行时报错说没有空参构造。骗子 + 2。
javap -p EnumSingle.class
Compiled from "EnumSingle.java"
public final class com.geek.singleton.EnumSingle extends java.lang.Enum<com.geek.singleton.EnumSingle> {
public static final com.geek.singleton.EnumSingle INSTANCE;
private static final com.geek.singleton.EnumSingle[] $VALUES;
public static com.geek.singleton.EnumSingle[] values();
public static com.geek.singleton.EnumSingle valueOf(java.lang.String);
private com.geek.singleton.EnumSingle();
public com.geek.singleton.EnumSingle getInstance();
static {};
}
使用 jad 反编译。
https://varaneckas.com/jad/jad158g.win.zip
jad -sjava EnumSingle.class。
可以看到 有参构造器。
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: EnumSingle.java
package com.geek.singleton;
public final class EnumSingle extends Enum
{
public static EnumSingle[] values()
{
return (EnumSingle[])$VALUES.clone();
}
public static EnumSingle valueOf(String name)
{
return (EnumSingle)Enum.valueOf(com/geek/singleton/EnumSingle, name);
}
private EnumSingle(String s, int i)
{
super(s, i);
}
public EnumSingle getInstance()
{
return INSTANCE;
}
public static final EnumSingle INSTANCE;
private static final EnumSingle $VALUES[];
static
{
INSTANCE = new EnumSingle("INSTANCE", 0);
$VALUES = (new EnumSingle[] {
INSTANCE
});
}
}
*/
Runtime 类 ~ 单例设计模式。
饿汉式(静态属性)。
/*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.lang;
import java.io.*;
import java.util.StringTokenizer;
import sun.reflect.CallerSensitive;
import sun.reflect.Reflection;
/**
* Every Java application has a single instance of class
* <code>Runtime</code> that allows the application to interface with
* the environment in which the application is running. The current
* runtime can be obtained from the <code>getRuntime</code> method.
* <p>
* An application cannot create its own instance of this class.
*
* @author unascribed
* @see java.lang.Runtime#getRuntime()
* @since JDK1.0
*/
public class Runtime {
private static Runtime currentRuntime = new Runtime();
/**
* Returns the runtime object associated with the current Java application.
* Most of the methods of class <code>Runtime</code> are instance
* methods and must be invoked with respect to the current runtime object.
*
* @return the <code>Runtime</code> object associated with the current
* Java application.
*/
public static Runtime getRuntime() {
return currentRuntime;
}
/** Don't let anyone else instantiate this class */
private Runtime() {}
package com.geek.singleton.pattern.demo;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
public class RuntimeDemo {
public static void main(String[] args) {
// 获取 Runtime 对象。
Runtime runtime = Runtime.getRuntime();
// 执行 dos 命令。
Process process = null;
try {
process = runtime.exec("ipconfig");
} catch (IOException e) {
// throw new RuntimeException(e);。
}
System.out.println("process = " + process);
// 调用 process 对象的获取输入流的方法。
InputStream inputStream = process.getInputStream();
byte[] bytes = new byte[1024 * 1024 * 100];
// 读取数据。返回读到的字节长度。
int length = 0;
try {
length = inputStream.read(bytes);
} catch (IOException e) {
// throw new RuntimeException(e);。
}
// 将字节数组转换为字符串输出到控制台。
try {
System.out.println(new String(bytes, 0, length, "gbk"));
} catch (UnsupportedEncodingException e) {
// throw new RuntimeException(e);。
}
}
}