单例设计模式定义:确保这个类只有一个实例,并且自动的实例化向系统提供这个对象。
package com.model.test;public class Singleton { // 使用静态变量记录唯一实例 private static Singleton singleton = null; private Singleton (){} public static Singleton getInstance (){ if (singleton == null){ singleton = new Singleton() ; } return singleton ; } public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance() ; Singleton singleton2 = Singleton.getInstance() ; /** * com.model.test.Singleton@15db9742 * com.model.test.Singleton@15db9742 */ System.out.println(singleton1); System.out.println(singleton2); }}
package com.model.test;
public class Singleton {
// 使用静态变量记录唯一实例
private static Singleton singleton = null;
private Singleton (){}
public static Singleton getInstance (){
if (singleton == null){
singleton = new Singleton() ;
}
return singleton ;
public static void main(String[] args) {
Singleton singleton1 = Singleton.getInstance() ;
Singleton singleton2 = Singleton.getInstance() ;
/**
* com.model.test.Singleton@15db9742
*/
System.out.println(singleton1);
System.out.println(singleton2);
Singleton称为单例类,构造函数使用private修饰,确保系统中只能产生一个实例,并且自动生成的。上面代码也就是所谓的懒汉式加载:只有到使用该对象的时候才来创建,意思饿了才来做饭吃。
在上面的代码中存在一个很明显的线程安全问题,当有多条线程来请求对象实例的时候,因为对象的创建是需要时间的,假设A线程进来判断singleton == null,就会进入对象的创建过程,这时如果同时在过来几条线程,那么他们都会得到一个对象实例,这个就是所谓的线程安全问题。
package com.model.test;public class Singleton { // 使用静态变量记录唯一实例 private static Singleton singleton = null; private Singleton (){} public static synchronized Singleton getInstance (){ if (singleton == null){ singleton = new Singleton() ; } return singleton ; }}
public static synchronized Singleton getInstance (){
这样操作会影响系统性能
public class Singleton { // 使用静态变量记录唯一实例 private static Singleton singleton = new Singleton(); private Singleton (){} public static Singleton getInstance (){ return singleton ; }}
private static Singleton singleton = new Singleton();
这里先把对象创建出来,有需要直接使用;
public class Singleton { // 使用静态变量记录唯一实例 // volatile可以确保当singleton被初始化后,多线程才可以正确处理 // 被volatile修饰的变量的值,将不会被本地线程缓存 // 对该变量读写都是直接操作共享内存,确保多个线程能正确的处理该变量。 private static volatile Singleton singleton = null ; private Singleton (){} public static Singleton getInstance (){ // 如果实例不存在,则进入同步区 if (singleton == null){ // 只有第一次才会彻底执行这里面的代码 synchronized (Singleton.class) { if (singleton == null){ singleton = new Singleton() ; } } } return singleton ; }}
// volatile可以确保当singleton被初始化后,多线程才可以正确处理
// 被volatile修饰的变量的值,将不会被本地线程缓存
// 对该变量读写都是直接操作共享内存,确保多个线程能正确的处理该变量。
private static volatile Singleton singleton = null ;
// 如果实例不存在,则进入同步区
// 只有第一次才会彻底执行这里面的代码
synchronized (Singleton.class) {
1)、类级内部类 简单点说,类级内部类指的是,有static修饰的成员式内部类。如果没有static修饰的成员式内部类被称为对象级内部类。 类级内部类相当于其外部类的static成分,它的对象与外部类对象间不存在依赖关系,因此可直接创建。而对象级内部类的实例,是绑定在外部对象实例中的。 类级内部类中,可以定义静态的方法。在静态方法中只能够引用外部类中的静态成员方法或者成员变量。 类级内部类相当于其外部类的成员,只有在第一次被使用的时候才被会装载。
2)、多线程缺省同步锁 在多线程开发中,为了解决并发问题,主要是通过使用synchronized来加互斥锁进行同步控制。但是在某些情况中,JVM已经隐含地执行了同步,这些情况下就不用自己再来进行同步控制了。这些情况包括:
1.由静态初始化器(在静态字段上或static{}块中的初始化器)初始化数据时 2.访问final字段时 3.在创建线程之前创建对象时 4.线程可以看见它将要处理的对象时
1.由静态初始化器(在静态字段上或static{}块中的初始化器)初始化数据时
2.访问final字段时
3.在创建线程之前创建对象时
4.线程可以看见它将要处理的对象时
要想很简单地实现线程安全,可以采用静态初始化器的方式,它可以由JVM来保证线程的安全性。比如前面的饿汉式实现方式,在类装载的时候就初始化对象,不管是否需要,存在一定的空间浪费。 一种可行的方式就是采用类级内部类,在这个类级内部类里面去创建对象实例。这样一来,只要不使用到这个类级内部类,那就不会创建对象实例,从而同时实现延迟加载和线程安全。
public class LazySingleton { /** * 类级内部类 */ private static class SingletonHolder { private static LazySingleton lazySingleton = new LazySingleton() ; } public static LazySingleton getInstance (){ return SingletonHolder.lazySingleton ; } public static void main(String[] args) { LazySingleton lazySingleton1 = LazySingleton.getInstance() ; LazySingleton lazySingleton2 = LazySingleton.getInstance() ; /** * com.model.test.LazySingleton@15db9742 * com.model.test.LazySingleton@15db9742 */ System.out.println(lazySingleton1+";;"+lazySingleton2); }}
public class LazySingleton {
* 类级内部类
private static class SingletonHolder {
private static LazySingleton lazySingleton = new LazySingleton() ;
public static LazySingleton getInstance (){
return SingletonHolder.lazySingleton ;
LazySingleton lazySingleton1 = LazySingleton.getInstance() ;
LazySingleton lazySingleton2 = LazySingleton.getInstance() ;
* com.model.test.LazySingleton@15db9742
System.out.println(lazySingleton1+";;"+lazySingleton2);
public class UserBean {}
public class UserBean {
<!-- 单例Bean --><bean id="user" class="com.model.design.spring.node01.singleton.UserBean" />
<!-- 单例Bean -->
<bean id="user" class="com.model.design.spring.node01.singleton.UserBean" />
package com.model.design.spring.node01.singleton;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Spring框架中单例模式 */public class S01_Singleton { @Test public void test01 (){ ApplicationContext context01 = new ClassPathXmlApplicationContext("/spring/spring-context.xml"); ApplicationContext context02 = new ClassPathXmlApplicationContext("/spring/spring-context.xml"); UserBean user01 = (UserBean)context01.getBean("user") ; UserBean user02 = (UserBean)context01.getBean("user") ; UserBean user03 = (UserBean)context02.getBean("user") ; // com.model.design.spring.node01.singleton.UserBean@364841 System.out.println(user01); // com.model.design.spring.node01.singleton.UserBean@364841 System.out.println(user02); // com.model.design.spring.node01.singleton.UserBean@c4711c System.out.println(user03); }}
package com.model.design.spring.node01.singleton;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
* Spring框架中单例模式
public class S01_Singleton {
@Test
public void test01 (){
ApplicationContext context01 = new ClassPathXmlApplicationContext("/spring/spring-context.xml");
ApplicationContext context02 = new ClassPathXmlApplicationContext("/spring/spring-context.xml");
UserBean user01 = (UserBean)context01.getBean("user") ;
UserBean user02 = (UserBean)context01.getBean("user") ;
UserBean user03 = (UserBean)context02.getBean("user") ;
// com.model.design.spring.node01.singleton.UserBean@364841
System.out.println(user01);
System.out.println(user02);
// com.model.design.spring.node01.singleton.UserBean@c4711c
System.out.println(user03);
结论 Spring单例模式与纯粹的单例设计模式的主要区别 尽管使用相同的类加载器来加载两个应用程序上下文,但是UserBean的实例是不一样的。也就是Spring框架中的单例对象是基于应用程序中。
优点: 1、单例模式只会创建一个对象实例,减少内存消耗 2、设置全局访问点,优化共享资源的访问
缺点: 1、没有接口,很难扩展 2、不利于测试 3、与单一职责原则冲突
GitHub地址:知了一笑https://github.com/cicadasmile/model-arithmetic-parent码云地址:知了一笑https://gitee.com/cicadasmile/model-arithmetic-parent
GitHub地址:知了一笑
https://github.com/cicadasmile/model-arithmetic-parent
码云地址:知了一笑
https://gitee.com/cicadasmile/model-arithmetic-parent
原文链接:http://www.cnblogs.com/cicada-smile/p/11146181.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728