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