| package com.example.pattern.Singleton;
import org.junit.Test;
import java.util.HashMap; import java.util.HashSet;
public class SingletonTest { final static Integer ThreadCount = 10; HashMap singletonHashMap = new HashMap<>();
@Test public void test(){ Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1); System.out.println(singleton2); System.out.println(singleton1==singleton2); } // 0、原始写法 存在线程安全 @Test public void testTheadSafe() throws InterruptedException { Thread thread1 = new Thread(new Runnable() { @Override public void run() { Singleton singleton = Singleton.getInstance(); System.out.println(singleton); } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { Singleton singleton = Singleton.getInstance(); System.out.println(singleton); } }); thread1.start(); thread2.start(); thread1.join(); thread2.join(); } // 1、懒汉式,线程安全解决 @Test public void testTheadSafeResolve() throws InterruptedException { for (int i=0; i< ThreadCount; i++){ Thread thread = new Thread(new Runnable() { @Override public void run() { Singleton singleton = Singleton.getInstanceForThreadSafe(); System.out.println(singleton); singletonHashMap.put(Thread.currentThread().getName(),singleton); } },"ThreadId="+Thread.currentThread().getName()); thread.start(); thread.join(); } HashSet hashSet = new HashSet(singletonHashMap.values()); if (hashSet.size() != singletonHashMap.values().size() ){ System.err.println("存在重复的单例对象"); }else { System.out.println("不存在重复的单例对象"); } }
// 2、双重检验锁(double checked locking) DCL @Test public void testDCL() throws InterruptedException { for (int i=0; i< ThreadCount; i++){ Thread thread = new Thread(new Runnable() { @Override public void run() { Singleton singleton = Singleton.getSingleton(); System.out.println(singleton); singletonHashMap.put(Thread.currentThread().getName(),singleton); } },"ThreadId="+Thread.currentThread().getName()); thread.start(); thread.join(); } HashSet hashSet = new HashSet(singletonHashMap.values()); if (hashSet.size() != singletonHashMap.values().size() ){ System.err.println("存在重复的单例对象"); }else { System.out.println("不存在重复的单例对象"); } }
// 3、饿汉式 static final field @Test public void testStatic() throws InterruptedException { for (int i=0; i< ThreadCount; i++){ Thread thread = new Thread(new Runnable() { @Override public void run() { Singleton singleton = Singleton.getInstanceStatic(); System.out.println(singleton); singletonHashMap.put(Thread.currentThread().getName(),singleton); } },"ThreadId="+Thread.currentThread().getName()); thread.start(); thread.join(); } HashSet hashSet = new HashSet(singletonHashMap.values()); if (hashSet.size() != singletonHashMap.values().size() ){ System.err.println("存在重复的单例对象"); }else { System.out.println("不存在重复的单例对象"); } }
// 4、静态内部类 static nested class @Test public void testStaticClass() throws InterruptedException { for (int i=0; i< ThreadCount; i++){ Thread thread = new Thread(new Runnable() { @Override public void run() { Singleton singleton = Singleton.getInstanceClass(); System.out.println(singleton); singletonHashMap.put(Thread.currentThread().getName(),singleton); } },"ThreadId="+Thread.currentThread().getName()); thread.start(); thread.join(); } HashSet hashSet = new HashSet(singletonHashMap.values()); if (hashSet.size() != singletonHashMap.values().size() ){ System.err.println("存在重复的单例对象"); }else { System.out.println("【不存在重复的单例对象】"); } } } |