设计模式---单例模式(多线程下的单例模式)

1>单例类

package com.test.sigleton;

public class SingletonTest {
	public static int num=0;//用于记录该类被实例化的次数  
    //声明一个类变量,外部代码想要得到SingletonTest对象,则返回该对象  
    private static SingletonTest st =null;  
    //将构造函数声明为private ,防止在外部代码直接new SingletonTest对象  
    private SingletonTest(){  
        num++;  
        System.out.println("----"+Thread.currentThread().getName()+"--"+"SingletonTest()---"+num);
    }  
      
    //得到SingletonTest类对象的方法  
    /*  
     *由于该类的构造方法声明为private的,所以不能在外部代码直接new一个该类的对象,  
     *所以也就不能调用实例方法,所以将getInsetance()方法声明为实例方法也就没有意义了,  
     *因为没有实例对象的话,也就不能调用该对象的实例方法。  
     *也是因为不能在外部直接new该类的对象,所以,外部能够访问该的方法只有类方法与类变量了,  
     *所以要将getInstance方法声明为类方法,也即static方法。  
     */  
    public static  SingletonTest getInstance (){  
          
        if(st==null){//当st对象不空时,创建一个  
        		System.out.println(Thread.currentThread().getName()+"---getInstance()---null");
            st=new SingletonTest();  
        }  
        System.out.println("----"+Thread.currentThread().getName()+"--"+"getInstance ()----"+num);  
        return st;  
    }  

}

2>线程类

package com.test.sigleton;

public class ThreadTest implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
			SingletonTest singletonTest = SingletonTest.getInstance();
		}
	}

}

3>测试类

package com.test.sigleton;

public class ThreadTest implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
			SingletonTest singletonTest = SingletonTest.getInstance();
		}
	}

}

运行结果:

Thread-2---getInstance()---null
Thread-1---getInstance()---null
----Thread-2--SingletonTest()---1
----Thread-1--SingletonTest()---2
----Thread-2--getInstance ()----2
----Thread-1--getInstance ()----2
----Thread-2--getInstance ()----2
----Thread-2--getInstance ()----2
----Thread-1--getInstance ()----2
----Thread-1--getInstance ()----2
----Thread-1--getInstance ()----2
----Thread-1--getInstance ()----2
----Thread-1--getInstance ()----2
----Thread-1--getInstance ()----2
----Thread-2--getInstance ()----2
----Thread-1--getInstance ()----2
----Thread-2--getInstance ()----2
----Thread-1--getInstance ()----2
----Thread-2--getInstance ()----2
----Thread-1--getInstance ()----2
----Thread-2--getInstance ()----2
----Thread-2--getInstance ()----2
----Thread-2--getInstance ()----2
----Thread-2--getInstance ()----2
通过上述结果可以看出在多线程(本例为两个线程+一个主线程)下,单例模式还是使“单例类”实例化了多次(两次),所以此单例模式在多线程下是失败的。

4>解决方法

4.1>同步方法---如果gitInstance()对程序的性能不是很关键,可以采用这种方法

public static SingletonTest getInstance ()方法声明为同步方法

public static synchronized SingletonTest getInstance ()即可

运行结果如下:

Thread-1---getInstance()---null
----Thread-1--SingletonTest()---1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1

4.2>使用"急切"创建实例,而不用延迟实例化的做法

我们依赖JVM在加载该类时马上创建一个该类的唯一实例。JVM保证了在任何线程想到该的实例时,就已经创建了该实例。

将单例类更改为如下:

package com.test.sigleton;

public class SingletonTest {
	public static int num=0;//用于记录该类被实例化的次数  
    //声明一个类变量,外部代码想要得到SingletonTest对象,则返回该对象  
	// 在静态初始化器中创建该类的实例---而不是延迟实例化
    private static SingletonTest st =new SingletonTest();  
    //将构造函数声明为private ,防止在外部代码直接new SingletonTest对象  
    private SingletonTest(){  
        num++;  
        System.out.println("----"+Thread.currentThread().getName()+"--"+"SingletonTest()---"+num);
    }  
      
    //得到SingletonTest类对象的方法  
    /*  
     *由于该类的构造方法声明为private的,所以不能在外部代码直接new一个该类的对象,  
     *所以也就不能调用实例方法,所以将getInsetance()方法声明为实例方法也就没有意义了,  
     *因为没有实例对象的话,也就不能调用该对象的实例方法。  
     *也是因为不能在外部直接new该类的对象,所以,外部能够访问该的方法只有类方法与类变量了,  
     *所以要将getInstance方法声明为类方法,也即static方法。  
     */  
    public static synchronized SingletonTest getInstance (){  
      /*    
       * 由于类加载器加载该类的时候就会实例化该类的一个实例,所以直接将该实例返回即可
        if(st==null){//当st对象不空时,创建一个  
        		System.out.println(Thread.currentThread().getName()+"---getInstance()---null");
            st=new SingletonTest();  
        }  */
        System.out.println("----"+Thread.currentThread().getName()+"--"+"getInstance ()----"+num);  
        return st;  
    }  

}

4.3>"双重检查加锁",在getInstance()中减少使用同步。

利用"双重检查加锁"(double-checked locking ),首先检查实例是否已经创建好,如果尚未创建,才进行同步。这样一来,只有第一次才会进行同步,这样以后再想得到该类的实例,由于已经创建好了,不必再创建了,所以对应用程序的效率影响几乎没有。

package com.test.sigleton;

public class SingletonTest {
	public static int num = 0;// 用于记录该类被实例化的次数
	// 声明一个类变量,外部代码想要得到SingletonTest对象,则返回该对象
	// 其中 volatile 关键字确保,当st变量被初始化时,多个线程正确的处理st 变量
	private volatile static SingletonTest st = null;

	// 将构造函数声明为private ,防止在外部代码直接new SingletonTest对象
	private SingletonTest() {
		num++;
		System.out.println("----" + Thread.currentThread().getName() + "--"
				+ "SingletonTest()---" + num);
	}

	// 得到SingletonTest类对象的方法
	/*
	 * 由于该类的构造方法声明为private的,所以不能在外部代码直接new一个该类的对象,
	 * 所以也就不能调用实例方法,所以将getInsetance()方法声明为实例方法也就没有意义了,因为没有实例对象的话,也就不能调用该对象的实例方法。
	 * 也是因为不能在外部直接new该类的对象,所以,外部能够访问该的方法只有类方法与类变量了,
	 * 所以要将getInstance方法声明为类方法,也即static方法。
	 */
	public static  SingletonTest getInstance() {
		if (st == null) {// 当st对象不空时,创建一个
			synchronized (SingletonTest.class) {
				if(st==null){
					System.out.println(Thread.currentThread().getName()
							+ "---getInstance()---null");
					st = new SingletonTest();
				}
			}
		}
		System.out.println("----" + Thread.currentThread().getName() + "--"
				+ "getInstance ()----" + num);
		return st;
	}

}

运行结果:

Thread-1---getInstance()---null
----Thread-1--SingletonTest()---1
----Thread-1--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-2--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1
----Thread-1--getInstance ()----1




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值