java single instance_java单例模式(具体代码显现)两种方法
判斷是否存在
/**
* 懶漢式
*/
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单例模式(具体代码显现)两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 索引常用注意事项
- 下一篇: 解决下载了pygame后,pycharm