java single instance_java单例模式(具体代码显现)两种方法
判斷是否存在
/**
* 懶漢式
*/
public class LazySingleInstance {
// 私有構(gòu)造方法
private LazySingleInstance(){};
// 私有的類對(duì)象
private static LazySingleInstance instance = null;
// 缺點(diǎn):
// 1 每次都需要去判斷instance 是否為空
// 2 調(diào)用時(shí)才去new對(duì)象,響應(yīng)比較慢
// 3 多線程時(shí),線程不安全,多線程同時(shí)獲取該對(duì)象,線程不安全,可能會(huì)產(chǎn)生多個(gè)對(duì)象出來(lái)
public static LazySingleInstance getInstance() throws InterruptedException {
// 每次都需要判斷instance是否為空,浪費(fèi)資源
if (null == instance) {
Thread.sleep(1000);
instance = new LazySingleInstance();
}
return instance;
}
}
考慮線程
/**
* 懶漢式線程安全方法一
*/
public class ThreadSafeSingleInstance {
// 私有的靜態(tài)實(shí)例
private static ThreadSafeSingleInstance instance;
// 私有的構(gòu)造方法
private ThreadSafeSingleInstance(){};
/**
* 懶漢式線程安全方法
* 同步鎖保護(hù)
* 缺點(diǎn):
* 性能低下,且同時(shí)每次均需檢查實(shí)例是否存在
* @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)實(shí)例
private static ThreadSafeSingleInstance instance;
// 私有的構(gòu)造方法
private ThreadSafeSingleInstance(){};
/**
* 同步塊保護(hù)加二次判斷
* 解決問(wèn)題
*/
public static ThreadSafeSingleInstance getInstance() throws InterruptedException {
if (instance == null) {
// 進(jìn)方法后進(jìn)行實(shí)例判斷,若實(shí)例為空,進(jìn)入同步塊
// 多線程時(shí),多個(gè)線程會(huì)同時(shí)進(jìn)入到同步塊外面等待
// 此時(shí),一個(gè)線程拿到ThreadSafeSingleInstance.class 后,其他線程在synchronized塊外面等待
// 待鎖釋放時(shí),一個(gè)等待的線程拿到鎖,同時(shí)再次檢查實(shí)例是否為空
// 并決定是否要?jiǎng)?chuàng)建實(shí)例
synchronized (String.class) {//ThreadSafeSingleInstance.class) {
Thread.sleep(1000);
if (instance == null) {
instance = new ThreadSafeSingleInstance();
}
}
}
return instance;
}
}
每次都去new
/**
* 餓漢式
*/
public class HungrySingleInstance {
// 私有的構(gòu)造方法
private HungrySingleInstance(){};
// 靜態(tài)類變量,初始化類時(shí)變創(chuàng)建對(duì)象
private static HungrySingleInstance instance = new HungrySingleInstance();
// 或采用靜態(tài)塊形式初始化instance
static {
instance = new HungrySingleInstance();
}
/**
* 獲取實(shí)例方法
* 優(yōu)點(diǎn):
* 線程安全,且不存在懶漢式的二次判斷問(wèn)題
* 缺點(diǎn):
* 初始化即new 實(shí)例對(duì)象出來(lái),違背資源利用習(xí)慣(即不管用不用都初始化實(shí)例出來(lái))
* @return
*/
public static HungrySingleInstance getInstance() {
return instance;
}
}
測(cè)試類
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();
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java single instance_java单例模式(具体代码显现)两种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 索引常用注意事项
- 下一篇: 解决下载了pygame后,pycharm