单例模式
懒汉试单例
package com.Design.SingleCase;
/**
* @Description: $ 单例模式 饿汉示
* 特点:1静态私有的成员变量,2 有一个静态的开发的方法 并返回对象
* 好处:在系统加载这个类
* 优点:线程安全。
* 缺点:过早浪费资源。
* @Param: $
* @return: $ SingleTonA对象
* @Author: Mr.Wang
* @Date: $2021.3.29
*/
public class SingleTonA {
private static SingleTonA SingleTonA = new SingleTonA ();
public SingleTonA() {
}
public static SingleTonA getSingleTonA() {
return SingleTonA;
}
}
饿汉试
package com.Design.SingleCase;
/**
* @Description: $ 单例模式 懒汉示
* 特点:优点:使用时候才创建,资源节约
* 缺点:为线程安全要付出额外代价啊
* * @Param: $
* @return: $ SingleTonA对象
* @Author: Mr.WangBo
* @Date: $2021.3.29
*/
public class SingleTonB {
private static SingleTonB SingleTonB = null;
public SingleTonB() {
}
public static SingleTonB getSingleTonA() {
if (SingleTonB == null) {
SingleTonB = new SingleTonB ();
}
return SingleTonB;
}
}
测试
@Test
public void singleTonATest01() {
int i = SingleTonA.getSingleTonA ().hashCode ();
int i1 = SingleTonB.getSingleTonA ().hashCode ();
System.out.println ("i1 = " + i1);
System.out.println ("i = " + i);
}
结果
i1 = 1535128843
i = 1915503092
Process finished with exit code 0
单例进阶 双重检测机制 懒汉试
package com.Design.SingleCase;
/**
* @Description: 双重检测机制 懒汉试$
* @Param: $
* @return: $
* @Author: Mr.Wang
* @Date: 2021.31.29$
* class
*/
public class SingleTonDoubleCheck {
/**
* volatile 只可以用来修饰变量,不可以修饰方法以及类
* 保证了不同线程对该变量操作的内存可见性.
* (当一个线程修改了变量,其他使用次变量的线程可以立即知道这一修改).
* 禁止了指令重排序.保证编译器不改变对其操作顺序
*/
private volatile static SingleTonDoubleCheck singleTon = null;
private SingleTonDoubleCheck() {
System.out.println (this.getClass ().getName () + "\tcreate");
}
/**
* synchronized 保证同步
*/
/**
* 第一种
* <p>
* public synchronized static SingleTonDoubleCheck getInstance() {
* if (singleTon == null) {
* try {
* Thread.sleep (1000L);
* } catch (InterruptedException e) {
* e.printStackTrace ();
* System.err.println ("懒汉试 没有成功睡眠");
* }
* singleTon = new SingleTonDoubleCheck ();
* }
* return singleTon;
* }
*/
/**
* 第二种
* public static SingleTonDoubleCheck getInstance()
*/
public static SingleTonDoubleCheck getInstance() {
if (singleTon == null) {
synchronized (SingleTonDoubleCheck.class) {
if (singleTon == null) {
try {
Thread.sleep (1000L);
} catch (InterruptedException e) {
e.printStackTrace ();
}
singleTon = new SingleTonDoubleCheck ();
}
}
}
return singleTon;
}
}
双重检测机制 懒汉试 测试
@Test
public void SingleTonDoubleCheckTest01() {
new Thread (new Runnable () {
@Override
public void run() {
System.out.println (SingleTonDoubleCheck.getInstance ());
}
}).start ();
System.out.println (SingleTonDoubleCheck.getInstance ());
}
双重检测机制 懒汉试 测试结果
com.Design.SingleCase.SingleTonDoubleCheck create
com.Design.SingleCase.SingleTonDoubleCheck@722c41f4
com.Design.SingleCase.SingleTonDoubleCheck@722c41f4
Process finished with exit code 0
holder 推荐使用
package com.Design.SingleCase;
/**
* @Description: holder 持有者模式 推荐使用 采用静态的方式$
* @Param: $
* @return: $
* @Author: Mr.Wang
* @Date: 2021.03.29$
*/
public class SingleTonHolder {
// create 创建
private static class SingleTonInnerHolder {
private static SingleTonHolder singleTonHolder = new SingleTonHolder ();
}
public SingleTonHolder() {
System.out.println ("SingleTonHolder被创建了 create");
}
public static SingleTonHolder getInstance() {
System.out.println ("getInstance");
try {
Thread.sleep (3000L);
} catch (InterruptedException e) {
e.printStackTrace ();
}
return SingleTonInnerHolder.singleTonHolder;
}
}
测试
@Test
public void SingleTonHolderTest01() {
new Thread (new Runnable () {
@Override
public void run() {
System.out.println (SingleTonHolder.getInstance ());
}
}).start ();
System.out.println (SingleTonHolder.getInstance ());
}
结果
getInstance
getInstance
SingleTonHolder被创建了 create
com.Design.SingleCase.SingleTonHolder@722c41f4
com.Design.SingleCase.SingleTonHolder@722c41f4