日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java single instance_java单例模式(具体代码显现)两种方法

發(fā)布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java single instance_java单例模式(具体代码显现)两种方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

判斷是否存在

/**

* 懶漢式

*/

public class LazySingleInstance {

// 私有構造方法

private LazySingleInstance(){};

// 私有的類對象

private static LazySingleInstance instance = null;

// 缺點:

// 1 每次都需要去判斷instance 是否為空

// 2 調用時才去new對象,響應比較慢

// 3 多線程時,線程不安全,多線程同時獲取該對象,線程不安全,可能會產生多個對象出來

public static LazySingleInstance getInstance() throws InterruptedException {

// 每次都需要判斷instance是否為空,浪費資源

if (null == instance) {

Thread.sleep(1000);

instance = new LazySingleInstance();

}

return instance;

}

}

考慮線程

/**

* 懶漢式線程安全方法一

*/

public class ThreadSafeSingleInstance {

// 私有的靜態(tài)實例

private static ThreadSafeSingleInstance instance;

// 私有的構造方法

private ThreadSafeSingleInstance(){};

/**

* 懶漢式線程安全方法

* 同步鎖保護

* 缺點:

* 性能低下,且同時每次均需檢查實例是否存在

* @return

* @throws InterruptedException

*/

public synchronized static ThreadSafeSingleInstance getInstance() throws InterruptedException {

if (instance == null) {

Thread.sleep(1000);

instance = new ThreadSafeSingleInstance();

}

return instance;

}

}

/**

* 懶漢式線程安全方法二

*/

public class ThreadSafeSingleInstance {

// 私有的靜態(tài)實例

private static ThreadSafeSingleInstance instance;

// 私有的構造方法

private ThreadSafeSingleInstance(){};

/**

* 同步塊保護加二次判斷

* 解決問題

*/

public static ThreadSafeSingleInstance getInstance() throws InterruptedException {

if (instance == null) {

// 進方法后進行實例判斷,若實例為空,進入同步塊

// 多線程時,多個線程會同時進入到同步塊外面等待

// 此時,一個線程拿到ThreadSafeSingleInstance.class 后,其他線程在synchronized塊外面等待

// 待鎖釋放時,一個等待的線程拿到鎖,同時再次檢查實例是否為空

// 并決定是否要創(chuàng)建實例

synchronized (String.class) {//ThreadSafeSingleInstance.class) {

Thread.sleep(1000);

if (instance == null) {

instance = new ThreadSafeSingleInstance();

}

}

}

return instance;

}

}

每次都去new

/**

* 餓漢式

*/

public class HungrySingleInstance {

// 私有的構造方法

private HungrySingleInstance(){};

// 靜態(tài)類變量,初始化類時變創(chuàng)建對象

private static HungrySingleInstance instance = new HungrySingleInstance();

// 或采用靜態(tài)塊形式初始化instance

static {

instance = new HungrySingleInstance();

}

/**

* 獲取實例方法

* 優(yōu)點:

* 線程安全,且不存在懶漢式的二次判斷問題

* 缺點:

* 初始化即new 實例對象出來,違背資源利用習慣(即不管用不用都初始化實例出來)

* @return

*/

public static HungrySingleInstance getInstance() {

return instance;

}

}

測試類

public class SingleInstanceTest implements Runnable{

HungrySingleInstance hungrySingleInstance = null;

LazySingleInstance lazySingleInstance = null;

ThreadSafeSingleInstance threadSafeSingleInstance = null;

// @Override

// public void run() {

// hungrySingleInstance = HungrySingleInstance.getInstance();

// System.out.println("hungrySingleInstance.hashCode() " + hungrySingleInstance.hashCode());

//

// }

// @Override

// public void run() {

// try {

// lazySingleInstance = LazySingleInstance.getInstance();

// } catch (InterruptedException e) {

// e.printStackTrace();

// }

// System.out.println("lazySingleInstance.hashCode() " + lazySingleInstance.hashCode());

// }

@Override

public void run() {

try {

threadSafeSingleInstance = ThreadSafeSingleInstance.getInstance();

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("threadSafeSingleInstance() " + threadSafeSingleInstance.hashCode());

}

public static void main(String args[]) throws InterruptedException{

SingleInstanceTest singleInstanceTest = new SingleInstanceTest();

for (int i=0; i< 100; i++) {

Thread t = new Thread(singleInstanceTest, String.valueOf(i));

t.start();

}

}

}

總結

以上是生活随笔為你收集整理的java single instance_java单例模式(具体代码显现)两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。