餓漢模式:
/**
* 單例-餓漢模式(即靜態(tài)創(chuàng)建)
* 優(yōu)點:線程安全,實現(xiàn)簡單
* 缺點:SingleInstance 在類加載的時候就實例化了,即便不用。(如果不用的話會造成內(nèi)存浪費)
* @author Rambo(浩祥)
* @create 2017-09-21
**/
public final class SingleInstance1 {
private static SingleInstance1 singleInstance = new SingleInstance1();
private SingleInstance1(){}
public static SingleInstance1 getSingleInstance(){
return singleInstance;
}
}
懶漢模式1
/**
* 單例--懶漢模式1(即用到的時候再去創(chuàng)建)
* 優(yōu)點:不用場景下,比懶漢模式節(jié)省內(nèi)存
* 缺點:非線程安全,耗時比懶漢模式長
* @author Rambo(浩祥)
* @create 2017-08-25
**/
public final class SingleInstance2 {
private static SingleInstance2 singleInstance = null;
private SingleInstance2(){
}
public static SingleInstance2 getSingleInstance(){
try {
if (singleInstance == null){
// 休眠50ms構(gòu)造線程非安全的場景
Thread.sleep(50);
singleInstance = new SingleInstance2();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return singleInstance;
}
}
懶漢模式2
/**
* 單例-懶漢模式2
* 優(yōu)點:較懶漢模式1比線程安全
* 缺點:較懶漢模式1耗時,因為要進行線程同步
* @author Rambo(浩祥)
* @create 2017-09-21
**/
public class SingleInstance3 {
private static SingleInstance3 singleInstance = null;
private SingleInstance3(){
}
public static synchronized SingleInstance3 getSingleInstance(){
try {
if (singleInstance == null){
// 休眠50ms構(gòu)造線程非安全的場景
Thread.sleep(50);
singleInstance = new SingleInstance3();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return singleInstance;
}
}
懶漢模式3
/**
* 單例-懶漢模式3
* 優(yōu)點-線程安全,比懶漢模式2節(jié)約用時
* @author Rambo(浩祥)
* @create 2017-09-21
**/
public class SingleInstance4 {
private static SingleInstance4 singleInstance = null;
private SingleInstance4(){
}
public static SingleInstance4 getSingleInstance(){
try {
if (singleInstance == null){
// 休眠50ms構(gòu)造線程非安全的場景
Thread.sleep(50);
synchronized (SingleInstance4.class){
if (singleInstance == null){
singleInstance = new SingleInstance4();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return singleInstance;
}
}
懶漢模式4
/**
* 單例-懶漢模式4
*
* @author Rambo(浩祥)
* @create 2017-09-21
**/
public class SingleInstance5 {
private static class GetInstance{
private static SingleInstance5 singleInstance = new SingleInstance5();
}
private SingleInstance5(){
}
// 只有調(diào)用到該方法是GetInstance才會加載,singleInstance才會初始化
public static SingleInstance5 getSingleInstance(){
return GetInstance.singleInstance;
}
}
測試程序
驗證不同單例模式的實例獲取時間僻弹、內(nèi)存占用芽淡、線程安全等
import com.rambo.memory.MemoryCounter;
/**
* @author Rambo(浩祥)
* @create 2017-03-09
**/
public class Practice {
// MemoryCounter是自己寫的對象內(nèi)存的計算類
private static MemoryCounter memoryCounter = new MemoryCounter();
public static void main(String[] args) {
Thread getInstanceThread = new Thread(new Runnable() {
@Override
public void run() {
getInstance();
}
});
getInstanceThread.start();
Thread getInstanceThread2 = new Thread(new Runnable() {
@Override
public void run() {
getInstance();
}
});
getInstanceThread2.start();
}
private static void getInstance() {
long startTime = System.currentTimeMillis();
// SingleInstance1 singleInstance1 = SingleInstance1.getSingleInstance();
// System.out.println(Thread.currentThread().getName() + ":singleInstance1獲取對象消耗用時:" + (System.currentTimeMillis() - startTime));
// System.out.println(Thread.currentThread().getName() + ":singleInstance1對象hashCode:" + Integer.toString(singleInstance1.hashCode()));
// System.out.println(Thread.currentThread().getName() + ":singleInstance1線程對象大小:" + memoryCounter.estimate(singleInstance1));
// SingleInstance2 singleInstance2 = SingleInstance2.getSingleInstance();
// System.out.println(Thread.currentThread().getName() + ":singleInstance2獲取對象消耗用時:" + (System.currentTimeMillis() - startTime));
// System.out.println(Thread.currentThread().getName() + ":singleInstance2對象hashCode:" + Integer.toString(singleInstance2.hashCode()));
// System.out.println(Thread.currentThread().getName() + ":singleInstance2線程對象大小:" + memoryCounter.estimate(singleInstance2));
//
// SingleInstance3 singleInstance3 = SingleInstance3.getSingleInstance();
// System.out.println(Thread.currentThread().getName() + ":singleInstance3獲取對象消耗用時:" + (System.currentTimeMillis() - startTime));
// System.out.println(Thread.currentThread().getName() + ":singleInstance3對象hashCode:" + Integer.toString(singleInstance3.hashCode()));
// System.out.println(Thread.currentThread().getName() + ":singleInstance3線程對象大小:" + memoryCounter.estimate(singleInstance3));
//
// SingleInstance4 singleInstance4 = SingleInstance4.getSingleInstance();
// System.out.println(Thread.currentThread().getName() + ":singleInstance4獲取對象消耗用時:" + (System.currentTimeMillis() - startTime));
// System.out.println(Thread.currentThread().getName() + ":singleInstance4對象hashCode:" + Integer.toString(singleInstance4.hashCode()));
// System.out.println(Thread.currentThread().getName() + ":singleInstance4線程對象大小:" + memoryCounter.estimate(singleInstance4));
//
SingleInstance5 singleInstance5 = SingleInstance5.getSingleInstance();
System.out.println(Thread.currentThread().getName() + ":singleInstance5獲取對象消耗用時:" + (System.currentTimeMillis() - startTime));
System.out.println(Thread.currentThread().getName() + ":singleInstance5對象hashCode:" + Integer.toString(singleInstance5.hashCode()));
System.out.println(Thread.currentThread().getName() + ":singleInstance5線程對象大小:" + memoryCounter.estimate(singleInstance5));
}
}
運行結(jié)果如下:
注:耗時大于50的是因為加了sleep(50)模擬線程非安全
注:實際應(yīng)用建議應(yīng)用懶漢4的方式