设为首页 加入收藏

TOP

Java设计模式GOF之单例模式(一)
2017-04-07 10:25:33 】 浏览:3590
Tags:Java 设计模式 GOF 单例 模式

一、单例模式(Singleton)

1、单例模式应用场景:

  ①Servlet
  ②任务管理器
  ③链接池
  ④Spring中每个 bean 默认是单例
  ⑤网站计数器

2、单例要求

  ①构造器私有
  ②私有的静态变量
  ③公共的静态的可以访问私有的静态变量的方法


结论:由结果可以得知单例模式为一个面向对象的应用程序提供了对象惟一的访问点,不管它实现何种功能,整个应用程序都会同享一个实例对象。
二、单例模式的实现方式


1、饿汉式
  线程安全、立即加载、资源利用率低、调用效率高


package cn.com.zfc.gof01.singleton;


/**
*
* @title Singleton01
* @describe 饿汉式实现单例模式
* @author 张富昌
* @date 2017年3月27日上午8:40:02
*/
public class Singleton01 {


  // 天然的线程安全的,类加载是立即加载这个实例
  private static Singleton01 instance = new Singleton01();


  / 构造器私有化
  private Singleton01() {


  }


  // 方法没有同步,效率比较高
  public static Singleton01 getInstance() {
    return instance;
  }
}




2、懒汉式
  线程安全、延迟加载、资源利用率高、调用效率低


package cn.com.zfc.gof01.singleton;


/**
*
* @title Singleton02
* @describe 懒汉式实现单例模式
* @author 张富昌
* @date 2017年3月27日上午8:45:42
*/
public class Singleton02 {

  private static Singleton02 instance = null;


  //构造其私有化
  private Singleton02() {
  }


  //公共,同步,静态
  public static synchronized Singleton02 getInstance() {
    if (instance == null) {
      instance = new Singleton02();
    }
    return instance;
  }
}



3、双重检索式
  线程安全、延迟加载、资源利用率高、调用效率高、但不稳定


package cn.com.zfc.gof01.singleton;


/**
*
* @title Singleton03
* @describe 双重检测锁式实现单例模式(不建议使用)
* @author 张富昌
* @date 2017年3月27日上午8:52:59
*/
public class Singleton03 {


  private static Singleton03 instance = null;


  private Singleton03() {


  }


  public static Singleton03 getInstance() {
    if (instance == null) {
      Singleton03 sc;
      synchronized (Singleton03.class) {
        sc = instance;
        if (sc == null) {
          synchronized (Singleton03.class) {
            if (sc == null) {
              sc = new Singleton03();
            }
          }
          instance = sc;
        }
      }
    }
    return instance;
  }


}



4、静态内部类式(相比于懒汉式更好)
  线程安全、延迟加载、资源利用率高、调用效率高


package cn.com.zfc.gof01.singleton;


/**
*
* @title Singleton04
* @describe 静态内部类式实现单例模式
* @author 张富昌
* @date 2017年3月27日上午8:54:40
*/
public class Singleton04 {


  // 构造器私有化
  private Singleton04() {


  }


  // 静态内部类
  private static class StaticInnerClass {
    private static final Singleton04 INSTANCE = new Singleton04();
  }


  public static Singleton04 getInstance() {
    return StaticInnerClass.INSTANCE;
  }


}



5、枚举单例式(相比于饿汉式更好)
  线程安全、立即加载、可以天然的防止反射和反序列化


package cn.com.zfc.gof01.singleton;


/**
*
* @title Singleton05
* @describe 枚举式实现单例模式
* @author 张富昌
* @date 2017年3月27日上午9:01:59
*/
public enum Singleton05 {
  // 单例对象
  INSTANCE;


  // 如果要对该单例对象进行额外的操作,则加入方法
  public void singlrtonOperation() {


  }


}


三、测试多线程环境下五种创建单例模式的效率


package cn.com.zfc.gof01.singleton.test;


import java.util.concurrent.CountDownLatch;


import cn.com.zfc.gof01.singleton.Singleton05;


/**
*
* @title SingletonTest
* @describe 测试多线程环境下五种创建单例模式的效率
* @author 张富昌
* @date 2017年3月27日上午9:24:58
*/
public class SingletonTest {
  public static void main(String[] args) throws Exception {


    long start = System.currentTimeMillis();
    int threadNum = 10;
    // A synchronization aid that allows one or more threads to wait until a
    // set of operations being performed in other threads completes.
    // 计数器(一个线程执行完成就减1)
    final CountDownLatch countDownLatch = new CountDownLatch(threadNum);


    for (int i = 0; i < threadNum; i++) {
      // 多线程测试
      new Thread(new Runnable() {
        @Override
        public void run() {


          for (int i = 0; i < 1000000; i++) {
            // 饿汉式,
            // Object o = Singleton01.getInstance();
            // 懒汉式,最慢
            // Object o = Singleton02.getInstance();
            // 双重检测锁式,不稳定,不建议使用
            // Object o = Singleton03.getInstance();
            // 静态内部类
            // Object o = Singleton04.getInstance();
            // 枚举式
            Object o = Singleton05.INSTANCE;
          }


          // 一个线程执行完成就减1
          countDownLatch.countDown();
        }
      }).start();
    }


    // 阻塞
    countDownLatch.await(); // main线程阻塞,直到计数器变为0,才会继续往下执行!


    long end = System.currentTimeMill

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇线性表之顺序表C++实现 下一篇Java多线程的实现方法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目