本文主要介紹單例創(chuàng)建的集中方式和反射給單例造成的影響。
單例的定義
單例模式:保證一個(gè)類僅有一個(gè)實(shí)例對(duì)象,并且提供一個(gè)全局訪問(wèn)點(diǎn)。
單例的特點(diǎn)
- 單例類只能有一個(gè)實(shí)例對(duì)象
- 單例類必須自己創(chuàng)建自己的唯一實(shí)例
- 單例類必須對(duì)外提供一個(gè)訪問(wèn)該實(shí)例的方法
使用場(chǎng)景及優(yōu)點(diǎn)
優(yōu):
- 提供了對(duì)唯一實(shí)例的受控訪問(wèn)
- 保證了內(nèi)存中只有唯一實(shí)例,減少內(nèi)存開(kāi)銷,比如需要多次創(chuàng)建和銷毀實(shí)例的場(chǎng)景
- 避免對(duì)資源的多重占用,比如文件的寫(xiě)操作
缺:
- 沒(méi)有抽象層,接口,不能繼承,擴(kuò)展困難,違反了開(kāi)閉原則
- 單例類一般寫(xiě)在同一個(gè)類中,職責(zé)過(guò)重,違背了單一職責(zé)原則
應(yīng)用場(chǎng)景:
文件系統(tǒng);數(shù)據(jù)庫(kù)連接池的設(shè)計(jì);日志系統(tǒng)等 IO/生成唯一序列號(hào)/身份證/對(duì)象需要共享的情況,比如web中配置對(duì)象
實(shí)現(xiàn)單例
三步:
- 構(gòu)造函數(shù)私有化
- 在類內(nèi)部創(chuàng)建實(shí)例
- 提供本類實(shí)例的唯一全局訪問(wèn)點(diǎn),即唯一實(shí)例的方法
餓漢式:
public class Hungry {
// 構(gòu)造器私有,靜止外部new
private Hungry(){}
// 在類的內(nèi)部創(chuàng)建自己的實(shí)例
private static Hungry hungry = new Hungry();
// 獲取本類實(shí)例的唯一全局訪問(wèn)點(diǎn)
public static Hungry getHungry(){
return hungry;
}
}
懶漢式:
public class Lazy1 {
// 構(gòu)造器私有,靜止外部new
private Lazy1(){
System.out.println(Thread.currentThread().getName() + " 訪問(wèn)到了");
}
// 定義即可,不真正創(chuàng)建
private static Lazy1 lazy1 = null;
// 獲取本類實(shí)例的唯一全局訪問(wèn)點(diǎn)
public static Lazy1 getLazy1(){
// 如果實(shí)例不存在則new一個(gè)新的實(shí)例,否則返回現(xiàn)有的實(shí)例
if (lazy1 == null) {
lazy1 = new Lazy1();
}
return lazy1;
}
public static void main(String[] args) {
// 多線程訪問(wèn),看看會(huì)有什么問(wèn)題
for (int i = 0; i < 10; i++) {
new Thread(()->{
Lazy1.getLazy1();
}).start();
}
}
}
單線程環(huán)境下是沒(méi)有問(wèn)題的,但是多線程的情況下就會(huì)出現(xiàn)問(wèn)題
DCL 懶漢式:
方法上直接加鎖:
public static synchronized Lazy1 getLazy1(){
if (lazy1 == null) {
lazy1 = new Lazy1();
}
return lazy1;
}
縮小鎖范圍:
public static Lazy1 getLazy1(){
if (lazy1 == null) {
synchronized(Lazy1.class){
lazy1 = new Lazy1();
}
}
return lazy1;
}
雙重鎖定:
// 獲取本類實(shí)例的唯一全局訪問(wèn)點(diǎn)
public static Lazy1 getLazy1(){
// 如果實(shí)例不存在則new一個(gè)新的實(shí)例,否則返回現(xiàn)有的實(shí)例
if (lazy1 == null) {
// 加鎖
synchronized(Lazy1.class){
// 第二次判斷是否為null
if (lazy1 == null){
lazy1 = new Lazy1();
}
}
}
return lazy1;
}
指令重排序: 指令重排序是JVM為了優(yōu)化指令,提高程序運(yùn)行效率,在不影響單線程程序執(zhí)行結(jié)果的前提下,盡可能地提高并行度。
首先要知道 lazy1 = new Lazy1(); 這一步并不是一個(gè)原子性操作,也就是說(shuō)這個(gè)操作會(huì)分成很多步
① 分配對(duì)象的內(nèi)存空間 ② 執(zhí)行構(gòu)造函數(shù),初始化對(duì)象 ③ 指向?qū)ο蟮絼偡峙涞膬?nèi)存空間
但是 JVM 為了效率對(duì)這個(gè)步驟進(jìn)行了重排序,例如這樣:
① 分配對(duì)象的內(nèi)存空間 ③ 指向?qū)ο蟮絼偡峙涞膬?nèi)存空間,對(duì)象還沒(méi)被初始化 ② 執(zhí)行構(gòu)造函數(shù),初始化對(duì)象
解決的方法很簡(jiǎn)單——在定義時(shí)增加 volatile 關(guān)鍵字,避免指令重排
最終代碼:
public class Lazy1 {
// 構(gòu)造器私有,靜止外部new
private Lazy1(){
System.out.println(Thread.currentThread().getName() + " 訪問(wèn)到了");
}
// 定義即可,不真正創(chuàng)建
private static volatile Lazy1 lazy1 = null;
// 獲取本類實(shí)例的唯一全局訪問(wèn)點(diǎn)
public static Lazy1 getLazy1(){
// 如果實(shí)例不存在則new一個(gè)新的實(shí)例,否則返回現(xiàn)有的實(shí)例
if (lazy1 == null) {
// 加鎖
synchronized(Lazy1.class){
// 第二次判斷是否為null
if (lazy1 == null){
lazy1 = new Lazy1();
}
}
}
return lazy1;
}
public static void main(String[] args) {
// 多線程訪問(wèn),看看會(huì)有什么問(wèn)題
for (int i = 0; i < 10; i++) {
new Thread(()->{
Lazy1.getLazy1();
}).start();
}
}
}
靜態(tài)內(nèi)部類懶漢式單例:
雙重鎖定算是一種可行不錯(cuò)的方式,而靜態(tài)內(nèi)部類就是一種更加好的方法,不僅速度較快,還保證了線程安全,先看代碼:
public class Lazy2 {
// 構(gòu)造器私有,靜止外部new
private Lazy2(){
System.out.println(Thread.currentThread().getName() + " 訪問(wèn)到了");
}
// 用來(lái)獲取對(duì)象
public static Lazy2 getLazy2(){
return InnerClass.lazy2;
}
// 創(chuàng)建內(nèi)部類
public static class InnerClass {
// 創(chuàng)建單例對(duì)象
private static Lazy2 lazy2 = new Lazy2();
}
public static void main(String[] args) {
// 多線程訪問(wèn),看看會(huì)有什么問(wèn)題
for (int i = 0; i < 10; i++) {
new Thread(()->{
Lazy2.getLazy2();
}).start();
}
}
}
上面的代碼,首先 InnerClass 是一個(gè)內(nèi)部類,其在初始化時(shí)是不會(huì)被加載的,當(dāng)用戶執(zhí)行了 getLazy2() 方法才會(huì)加載,同時(shí)創(chuàng)建單例對(duì)象,所以他也是懶漢式的方法,因?yàn)?InnerClass 是一個(gè)靜態(tài)內(nèi)部類,所以只會(huì)被實(shí)例化一次,從而達(dá)到線程安全,因?yàn)椴](méi)有加鎖,所以性能上也會(huì)很快。
枚舉創(chuàng)建單例:
public enum EnumSingle {
IDEAL;
}
代碼就這樣,簡(jiǎn)直不要太簡(jiǎn)單,訪問(wèn)通過(guò) EnumSingle.IDEAL 就可以訪問(wèn)了
反射破壞單例模式
單例是如何被破壞的:
這是我們?cè)瓉?lái)的寫(xiě)法,new 兩個(gè)實(shí)例出來(lái),輸出一下
public class Lazy1 {
// 構(gòu)造器私有,靜止外部new
private Lazy1(){
System.out.println(Thread.currentThread().getName() + " 訪問(wèn)到了");
}
// 定義即可,不真正創(chuàng)建
private static volatile Lazy1 lazy1 = null;
// 獲取本類實(shí)例的唯一全局訪問(wèn)點(diǎn)
public static Lazy1 getLazy1(){
// 如果實(shí)例不存在則new一個(gè)新的實(shí)例,否則返回現(xiàn)有的實(shí)例
if (lazy1 == null) {
// 加鎖
synchronized(Lazy1.class){
// 第二次判斷是否為null
if (lazy1 == null){
lazy1 = new Lazy1();
}
}
}
return lazy1;
}
public static void main(String[] args) {
Lazy1 lazy1 = getLazy1();
Lazy1 lazy2 = getLazy1();
System.out.println(lazy1);
System.out.println(lazy2);
}
}
運(yùn)行結(jié)果: main 訪問(wèn)到了 cn.ideal.single.Lazy1@1b6d3586 cn.ideal.single.Lazy1@1b6d3586
可以看到,結(jié)果是單例沒(méi)有問(wèn)題
一個(gè)普通實(shí)例化,一個(gè)反射實(shí)例化:
public static void main(String[] args) throws Exception {
Lazy1 lazy1 = getLazy1();
// 獲得其空參構(gòu)造器
Constructor<Lazy1> declaredConstructor = Lazy1.class.getDeclaredConstructor(null);
// 使得可操作性該 declaredConstructor 對(duì)象
declaredConstructor.setAccessible(true);
// 反射實(shí)例化
Lazy1 lazy2 = declaredConstructor.newInstance();
System.out.println(lazy1);
System.out.println(lazy2);
}
運(yùn)行結(jié)果:
main 訪問(wèn)到了 main 訪問(wèn)到了 cn.ideal.single.Lazy1@1b6d3586 cn.ideal.single.Lazy1@4554617c
可以看到,單例被破壞了
如何解決:因?yàn)槲覀兎瓷渥叩钠錈o(wú)參構(gòu)造,所以在無(wú)參構(gòu)造中再次進(jìn)行非null判斷,加上原來(lái)的雙重鎖定,現(xiàn)在也就有三次判斷了。
解決方案:增加一個(gè)標(biāo)識(shí)位,例如下文通過(guò)增加一個(gè)布爾類型的 ideal 標(biāo)識(shí),保證只會(huì)執(zhí)行一次,更安全的做法,可以進(jìn)行加密處理,保證其安全性。
這樣就沒(méi)問(wèn)題了嗎,并不是,一旦別人通過(guò)一些手段得到了這個(gè)標(biāo)識(shí)內(nèi)容,那么他就可以通過(guò)修改這個(gè)標(biāo)識(shí)繼續(xù)破壞單例,代碼如下(這個(gè)把代碼貼全一點(diǎn),前面都是節(jié)選關(guān)鍵的,都可以參考這個(gè))
public class Lazy1 {
private static boolean ideal = false;
// 構(gòu)造器私有,靜止外部new
private Lazy1(){
synchronized (Lazy1.class){
if (ideal == false){
ideal = true;
} else {
throw new RuntimeException("反射破壞單例異常");
}
}
System.out.println(Thread.currentThread().getName() + " 訪問(wèn)到了");
}
// 定義即可,不真正創(chuàng)建
private static volatile Lazy1 lazy1 = null;
// 獲取本類實(shí)例的唯一全局訪問(wèn)點(diǎn)
public static Lazy1 getLazy1(){
// 如果實(shí)例不存在則new一個(gè)新的實(shí)例,否則返回現(xiàn)有的實(shí)例
if (lazy1 == null) {
// 加鎖
synchronized(Lazy1.class){
// 第二次判斷是否為null
if (lazy1 == null){
lazy1 = new Lazy1();
}
}
}
return lazy1;
}
public static void main(String[] args) throws Exception {
Field ideal = Lazy1.class.getDeclaredField("ideal");
ideal.setAccessible(true);
// 獲得其空參構(gòu)造器
Constructor<Lazy1> declaredConstructor = Lazy1.class.getDeclaredConstructor(null);
// 使得可操作性該 declaredConstructor 對(duì)象
declaredConstructor.setAccessible(true);
// 反射實(shí)例化
Lazy1 lazy1 = declaredConstructor.newInstance();
ideal.set(lazy1,false);
Lazy1 lazy2 = declaredConstructor.newInstance();
System.out.println(lazy1);
System.out.println(lazy2);
}
}
運(yùn)行結(jié)果: main 訪問(wèn)到了 main 訪問(wèn)到了 cn.ideal.single.Lazy1@4554617c cn.ideal.single.Lazy1@74a14482 實(shí)例化 lazy1 后,其執(zhí)行了修改 ideal 這個(gè)布爾值為 false,從而繞過(guò)了判斷,再次破壞了單例 所以,可以得出,這幾種方式都是不安全的,都有著被反射破壞的風(fēng)險(xiǎn)。