auto commit

This commit is contained in:
CyC2018
2018-09-29 22:34:48 +08:00
parent 668ee4e846
commit 6d5190805c
3 changed files with 13 additions and 6 deletions

View File

@ -143,7 +143,7 @@ if (uniqueInstance == null) {
}
```
uniqueInstance 采用 volatile 关键字修饰也是很有必要的`uniqueInstance = new Singleton();` 这段代码其实是分为三步执行
uniqueInstance 采用 volatile 关键字修饰也是很有必要的 `uniqueInstance = new Singleton();` 这段代码其实是分为三步执行
1. 为 uniqueInstance 分配内存空间
2. 初始化 uniqueInstance
@ -219,7 +219,14 @@ public enum Singleton {
}
```
该实现在多次序列化再进行反序列化之后,不会得到多个实例。而其它实现,为了保证不会出现反序列化之后出现多个实例,需要使用 transient 修饰所有字段,并且实现序列化和反序列化的方法。
```html
firstName
secondName
secondName
secondName
```
该实现在多次序列化再进行反序列化之后,不会得到多个实例。而其它实现需要使用 transient 修饰所有字段,并且实现序列化和反序列化的方法。
该实现可以防止反射攻击。在其它实现中,通过 setAccessible() 方法可以将私有构造函数的访问级别设置为 public然后调用构造函数从而实例化对象如果要防止这种攻击需要在构造函数中添加防止多次实例化的代码。该实现是由 JVM 保证只会实例化一次,因此不会出现上述的反射攻击。