1.新手写法 单线程安全 多线程 不安全 (千万不能用)
public class People {
private static People singleton;
private People() {
System.out.println(Thread.currentThread().getName()+" OK");
}
public static People getInstance() {
if (singleton == null) {
singleton = new People();
}
return singleton;
}
}
2. 静态方法加锁
资源浪费 性能不好
原因 锁的东西太多了
/**
*线程安全,
*
*/
public class Fish {
private static Fish fish;
private Fish() {
System.out.println(Thread.currentThread().getName()+" OK");
}
public static synchronized Fish getInstance() {
if (fish == null) {
fish = new Fish();
}
return fish;
}
}
3 ,双重检查加 禁止指令重排
/**
*双重检查锁 模式
*
*
*/
public class Tiger {
private static volatile Tiger tiger; //volatile 禁止指令从重排
private Tiger() {
System.out.println(Thread.currentThread().getName()+" OK");
}
public static Tiger getInstance() {
if (tiger == null) {
synchronized (Tiger.class) {
if (tiger == null) {
tiger = new Tiger();
/**
* 1 分配内存空间
* 2.执行构造方法 初始化对象
*3.把这个对象指向这个空间
* 指令重排现象
*/
}
}
}
return tiger;
}
}
以上方法防好人不妨坏人
因为Java 1.8 反射的存在
测试方法
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class TigerTest {
/**
* 采用多线程的方式
*
*/
@Test
public void threadTset() throws InterruptedException {
for(int i=0;i<10;i++)
{
new Thread(()->{
System.out.println(Tiger.getInstance().hashCode());
/* System.out.println(People.getInstance().hashCode());*/
}).start();
}
}
@Test //采用 反射 获得对象
public void reflection() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Tiger instance = Tiger.getInstance();
System.out.println(instance.hashCode());
Constructor<Tiger> declaredConstructor = Tiger.class.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true); //无视私有的构造器
Tiger tiger = declaredConstructor.newInstance();
System.out.println(tiger.hashCode());
}
}
4.静态内部类
/**
*
* 静态内部类
*/
public class Pig {
private Pig() {
System.out.println(Thread.currentThread().getName()+" OK");
}
private static class chicken {
private static final Pig PIG = new Pig();
}
public static Pig getInstance() {
return chicken.PIG;
}
}
5.加强版 双重检查锁
/**
*双重检查锁 模式
*
*
*/
public class TigerStrengthen {
private static Boolean NWK=false;
private static volatile TigerStrengthen tiger; //volatile 禁止指令从重排
private TigerStrengthen()
{
synchronized (TigerStrengthen.class)
{
/* if(tiger!=null)
{
throw new RuntimeException("小伙子很有想法");
}*/
if(NWK==false)
{
NWK=true;
}
else
{
throw new RuntimeException("小伙子很有想法");
}
}
}
public static TigerStrengthen getInstance() {
if (tiger == null) {
synchronized (TigerStrengthen.class) {
if (tiger == null) {
tiger = new TigerStrengthen();
/**
* 1 分配内存空间
* 2.执行构造方法 初始化对象
*3.把这个对象指向这个空间
* 指令重排现象
*/
}
}
}
return tiger;
}
}
测试方法
里面的方法和注释掉的代码有关
测试时请前找到对应的方法
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class TigerStrengthenTest {
/**
* 采用多线程的方式
*
*/
@Test
public void threadTset() throws InterruptedException {
for(int i=0;i<10;i++)
{
new Thread(()->{
System.out.println(TigerStrengthen.getInstance().hashCode());
}).start();
}
}
@Test
public void reflection() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
TigerStrengthen instance = TigerStrengthen.getInstance();
System.out.println(instance.hashCode());
Constructor<TigerStrengthen> declaredConstructor = TigerStrengthen.class.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true); //无视私有的构造器
TigerStrengthen tigerStrengthen = declaredConstructor.newInstance();
System.out.println(tigerStrengthen.hashCode());
}
@Test
public void reflectiontS() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor<TigerStrengthen> declaredConstructor = TigerStrengthen.class.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true); //无视私有的构造器
TigerStrengthen tigerStrengthen1 = declaredConstructor.newInstance();
TigerStrengthen tigerStrengthen2 = declaredConstructor.newInstance();
System.out.println(tigerStrengthen1.hashCode());
System.out.println(tigerStrengthen2.hashCode());
}
@Test
public void reflectiontSS() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
Field nwk = TigerStrengthen.class.getDeclaredField("NWK");
nwk.setAccessible(true);
Constructor<TigerStrengthen> declaredConstructor = TigerStrengthen.class.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true); //无视私有的构造器
TigerStrengthen tigerStrengthen1 = declaredConstructor.newInstance();
nwk.set(nwk,false);
TigerStrengthen tigerStrengthen2 = declaredConstructor.newInstance();
System.out.println(tigerStrengthen1.hashCode());
System.out.println(tigerStrengthen2.hashCode());
}
}
猜你感兴趣的
饿汉模式
https://blog.csdn.net/A980719/article/details/117429760
枚举实现单例
https://blog.csdn.net/A980719/article/details/117429536