最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Java利用static實(shí)現(xiàn)單例模式

 更新時(shí)間:2022年01月09日 10:11:23   作者:靈劍山真人  
這篇文章主要介紹了基于Java利用static實(shí)現(xiàn)單例模式,當(dāng)在多個(gè)線程同時(shí)觸發(fā)類的初始化過程的時(shí)候static不會(huì)被多次執(zhí)行,下面我們一起進(jìn)入文章看看具體要的原因

一、之前舊的寫法

class Singleton{
? ? private Singleton() {}
? ? private static Singleton instance = null;
? ? public synchronized static Singleton getInstance() {
? ? ? ? ? ? if (instance == null) {
? ? ? ? ? ? ? ? instance = new Singleton();
? ? ? ? ? ? }
? ? ? ? ? ? return instance;
? ? ? ? }
}

就利用Sington.getInstace就可以了,獲得的是同一個(gè)實(shí)例。

上面那個(gè)代碼有兩個(gè)優(yōu)點(diǎn):

  • 懶加載,把在堆創(chuàng)建實(shí)例這個(gè)行為延遲到類的使用時(shí)。
  • 鎖效果,防止生成多個(gè)實(shí)例,因?yàn)?code>synchronized修飾這個(gè)static方法,所以相當(dāng)于給這個(gè)方法加上了一個(gè)類鎖??。

二、static代碼塊的效果

先來看一段代碼:

 

class StaticClass{
? ? private static int a = 1;
? ? static{
? ? ? ? System.out.println("語句1");
? ? ? ? System.out.println("語句2");
? ? }
? ? static{
? ? ? ? System.out.println("語句3");
? ? ? ? System.out.println("語句4");
? ? }
}

當(dāng)在多個(gè)線程同時(shí)觸發(fā)類的初始化過程的時(shí)候(在初始化過程,類的static變量會(huì)被賦值為JVM默認(rèn)值并且static代碼塊會(huì)被執(zhí)行),為什么static不會(huì)被多次執(zhí)行?因?yàn)橛锌赡軆蓚€(gè)線程同時(shí)檢測到類還沒被初始化,然后都執(zhí)行static代碼塊,結(jié)果就把語句1234多打印了,為什么上述情況不會(huì)發(fā)生。

Thread thread1 = new Thread(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? Class.forName("StaticClass");//這一行觸發(fā)類的初始化導(dǎo)致靜態(tài)代碼塊執(zhí)行
? ? ? ? ? ? ? ? } catch (ClassNotFoundException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? thread1.start();
? ? ? ? Thread thread2 = new Thread(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? Class.forName("StaticClass");//同樣
? ? ? ? ? ? ? ? } catch (ClassNotFoundException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? thread2.start();

結(jié)果如下:

語句1
語句2
語句3
語句4

有一段英文對其進(jìn)行了解釋:

static initialization block can be triggered from multiple parallel threads (when the loading of the class happens in the first time), Java runtime guarantees that it will be executed only once and in thread-safe manner + when we have more than 1 static block - it guarantees the sequential execution of the blocks, 也就是說,java runtime幫我們做了兩件事:

  • 在并行線程中,都出現(xiàn)了第一次初始化類的情況,保證類的初始化只執(zhí)行一次。
  • 保證static代碼塊的順序執(zhí)行

三、單例的另一種寫法

有了對static的知識的了解之后,我們可以寫出這樣的單例模式:

class Singleton{
? ? private Singleton() {}
? ? private static class NestedClass{
? ? ? ?static Singleton instance = new Singleton();//這條賦值語句會(huì)在初始化時(shí)才運(yùn)行
? ? }
? ? public static Singleton getInstance() {
? ? ? ? return NestedClass.instance;
? ? }
}
  • 懶加載,因?yàn)?code>static語句會(huì)在初始化時(shí)才賦值運(yùn)行,達(dá)到了懶加載的效果。
  • 鎖??的效果由Java runtime保證,虛擬機(jī)幫我們保證static語句在初始化時(shí)只會(huì)執(zhí)行一次。

四、總結(jié)

如果不知道static的基礎(chǔ)知識和虛擬機(jī)類加載的知識,我可能并不會(huì)知道這一種方法。理論永遠(yuǎn)先行于技術(shù),要學(xué)好理論才能從根本上提升自己。

到此這篇關(guān)于基于Java利用static實(shí)現(xiàn)單例模式的文章就介紹到這了,更多相關(guān)static實(shí)現(xiàn)單例模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

扎赉特旗| 德惠市| 姚安县| 左云县| 汪清县| 上虞市| 郎溪县| 刚察县| 正安县| 京山县| 建阳市| 方正县| 屏山县| 姚安县| 哈尔滨市| 毕节市| 南岸区| 雅江县| 龙海市| 镇康县| 蛟河市| 定边县| 祁阳县| 荔浦县| 宜川县| 额尔古纳市| 蕲春县| 屯门区| 大城县| 连城县| 资中县| 永修县| 南丰县| 徐州市| 高邑县| 昌都县| 平遥县| 稷山县| 承德县| 西林县| 巴中市|