来源:http://www.bjsxt.com/
一、【GOF23设计模式】_单例模式、应用场景、饿汉式、懒汉式
1、GOF23设计模式
2、单例模式
3、饿汉式
1 package com.test.singleton; 2 /** 3 * 测试饿汉式单例模式 4 */ 5 public class SingletonDemo01 { 6 7 //类初始化时,立即加载这个对象(没有延时加载的优势)。加载类时,天然的是线程安全的! 8 private static SingletonDemo01 instance = new SingletonDemo01(); 9 10 private SingletonDemo01(){//私有化构造器 11 } 12 13 //方法不用同步,调用效率高! 14 public static SingletonDemo01 getInstance(){ 15 return instance; 16 } 17 }
4、懒汉式
1 package com.test.singleton; 2 /** 3 * 测试懒汉式单例模式 4 */ 5 public class SingletonDemo02 { 6 7 //类初始化时,不初始化这个对象(延时加载,真正用的时候再创建) 8 private static SingletonDemo02 instance; 9 10 private SingletonDemo02(){//私有化构造器 11 } 12 13 //方法同步,调用效率低! 14 public static synchronized SingletonDemo02 getInstance(){ 15 if(null==instance){ 16 instance = new SingletonDemo02(); 17 } 18 return instance; 19 } 20 }
二、【GOF23设计模式】_单例模式、双重检查锁式、静态内部类式、枚举式、UML类图
双重检测锁实现
1 package com.test.singleton; 2 /** 3 * 测试双重检测锁式单例模式 4 */ 5 public class SingletonDemo03 { 6 7 //类初始化时,不初始化这个对象(延时加载,真正用的时候再创建) 8 private static SingletonDemo03 instance; 9 10 private SingletonDemo03(){//私有化构造器 11 } 12 13 //调用效率低! 14 public static SingletonDemo03 getInstance(){ 15 if(null==instance){ 16 SingletonDemo03 sc; 17 synchronized(SingletonDemo03.class){ 18 sc = instance; 19 if(null==sc){ 20 synchronized (SingletonDemo03.class) { 21 if(null==sc){ 22 sc = new SingletonDemo03(); 23 } 24 } 25 instance = sc; 26 } 27 } 28 } 29 return instance; 30 } 31 }
静态内部类实现
1 package com.test.singleton; 2 /** 3 * 测试静态内部类实现单例模式 4 * 这种方式:线程安全,调用效率高,并且实现了延时加载! 5 */ 6 public class SingletonDemo04 { 7 8 private static class SingletonClassInstance { 9 private static final SingletonDemo04 instance = new SingletonDemo04(); 10 } 11 12 private SingletonDemo04(){//私有化构造器 13 } 14 15 //方法没有同步,调用效率高! 16 public static SingletonDemo04 getInstance(){ 17 return SingletonClassInstance.instance; 18 } 19 }
枚举实现
1 package com.test.singleton; 2 /** 3 * 测试枚举式实现单例模式(没有延时加载) 4 */ 5 public enum SingletonDemo05 { 6 7 //这个枚举元素,本身就是单例对象! 8 INSTANCE; 9 10 //添加自己需要的操作! 11 public void singletonOperation(){ 12 } 13 }
1 package com.test.singleton; 2 3 public class Client { 4 public static void main(String[] args) { 5 SingletonDemo01 s1 = SingletonDemo01.getInstance(); 6 SingletonDemo01 s2 = SingletonDemo01.getInstance(); 7 System.out.println(s1==s2);//true 8 9 SingletonDemo03 s3 = SingletonDemo03.getInstance(); 10 SingletonDemo03 s4 = SingletonDemo03.getInstance(); 11 System.out.println(s3==s4);//true 12 13 System.out.println(SingletonDemo05.INSTANCE==SingletonDemo05.INSTANCE);//true 14 } 15 }
五种单例模式实现
三、【GOF23设计模式】_单例模式、反射和反序列化漏洞和解决方案、多线程环境测试、CountDownLatch同步类的使用
1 package com.test.singleton; 2 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.ObjectInputStream; 6 import java.io.ObjectOutputStream; 7 import java.lang.reflect.Constructor; 8 9 /** 10 * 测试反射和反序列化破解单例模式 11 */ 12 public class Client2 { 13 public static void main(String[] args) throws Exception { 14 SingletonDemo06 s1 = SingletonDemo06.getInstance(); 15 SingletonDemo06 s2 = SingletonDemo06.getInstance(); 16 System.out.println(s1); 17 System.out.println(s2); 18 19 //通过反射的方式直接调用私有构造器 20 /* Class<SingletonDemo06> clazz = (Class<SingletonDemo06>) Class.forName("com.test.singleton.SingletonDemo06"); 21 Constructor<SingletonDemo06> c = clazz.getDeclaredConstructor(null); 22 c.setAccessible(true);//访问私有,破解单例 23 SingletonDemo06 s3 = c.newInstance(); 24 SingletonDemo06 s4 = c.newInstance(); 25 System.out.println(s3);//SingletonDemo06的私有构造器中加了代码防止破解 26 System.out.println(s4);*/ 27 28 //通过反序列化的方式构造多个对象 29 FileOutputStream fos = new FileOutputStream("g:/java/test/a.txt"); 30 ObjectOutputStream oos = new ObjectOutputStream(fos); 31 oos.writeObject(s1); 32 oos.close(); 33 fos.close(); 34 35 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("g:/java/test/a.txt")); 36 SingletonDemo06 s3 = (SingletonDemo06) ois.readObject(); 37 System.out.println(s3);//SingletonDemo06定义了readResolve()防止破解 38 } 39 }
1 package com.test.singleton; 2 3 import java.io.ObjectStreamException; 4 import java.io.Serializable; 5 6 /** 7 * 测试懒汉式单例模式(如何防止反射和反序列化漏洞) 8 */ 9 public class SingletonDemo06 implements Serializable{ 10 11 //类初始化时,不初始化这个对象(延时加载,真正用的时候再创建) 12 private static SingletonDemo06 instance; 13 14 private SingletonDemo06(){//私有化构造器 15 if(instance!=null){//防止反射破解单例(对象.setAccessible(true);) 16 throw new RuntimeException(); 17 } 18 } 19 20 //方法同步,调用效率低! 21 public static synchronized SingletonDemo06 getInstance(){ 22 if(null==instance){ 23 instance = new SingletonDemo06(); 24 } 25 return instance; 26 } 27 28 //反序列化时,如果定义了readResolve()则直接返回此方法指定的对象。而不需要单独再创建新对象! 29 private Object readResolve() throws ObjectStreamException{ 30 return instance; 31 } 32 }
1 package com.test.singleton; 2 3 import java.util.concurrent.CountDownLatch; 4 5 /** 6 * 测试多线程环境下五种创建单例模式的效率 7 */ 8 public class Client3 { 9 public static void main(String[] args) throws Exception{ 10 11 int threadNum = 10; 12 final CountDownLatch countDownLatch = new CountDownLatch(threadNum); 13 14 long start = System.currentTimeMillis(); 15 for(int i=0;i<10;i++){ 16 new Thread(new Runnable(){ 17 @Override 18 public void run() { 19 for (int i = 0; i < 1000000; i++) { 20 Object o = SingletonDemo01.getInstance();//分别测试几种单例模式的效率 21 //Object o = SingletonDemo05.INSTANCE; 22 } 23 24 countDownLatch.countDown();//计数器减1 25 } 26 }).start(); 27 } 28 29 countDownLatch.await();//main线程阻塞,直到计数器变为0,才会继续往下执行! 30 31 long end = System.currentTimeMillis(); 32 System.out.println("总耗时:" + (end-start)); 33 } 34 }
时间: 2024-10-11 17:53:48