标签:
有些时候在程序中只需要某个类只产生唯一的实例。例如:windows系统中的回收站对象。无论我们在哪个盘中删除数据,被删除的数据都是放在同一个回收站中。这里就用到了单例设计模式。
下面我们用代码演示一下JAVA中单例设计模式。其核心思想是将构造函数私有化,然后用静态方法获取实例。
RecycleBin类代码:
package org.lyk.demo;
public class RecycleBin
{
public static RecycleBin uniqueInstance;
private RecycleBin()
{}
public static RecycleBin getInstance()
{
if(null == RecycleBin.uniqueInstance)
{
RecycleBin.uniqueInstance = new RecycleBin();
}
return RecycleBin.uniqueInstance;
}
}
main方法:
package org.lyk.demo;
public class RecycleBin
{
public static RecycleBin uniqueInstance;
private RecycleBin()
{}
public static RecycleBin getInstance()
{
if(null == RecycleBin.uniqueInstance)
{
RecycleBin.uniqueInstance = new RecycleBin();
}
return RecycleBin.uniqueInstance;
}
}
以上代码输出结果如下:
bin1 == bin2 :true bin1.hashCode() : 31168322 bin2.hashCode() : 31168322
可以看到,bin1和bin2完全相同。
但是,以上的代码是有问题的。如果我们在写多线程的时候,就会遇到问题。多个多个线程同时去调用getInstance的时候有可能会产生多个实例。请看下面的代码:
RecycleBin类:
package org.lyk.demo; import java.lang.Thread; public class RecycleBin { public static RecycleBin uniqueInstance; private RecycleBin() {} public static RecycleBin getInstance() { if(null == RecycleBin.uniqueInstance) { try { Thread.sleep(3000); }catch(InterruptedException e) { e.printStackTrace(); } RecycleBin.uniqueInstance = new RecycleBin(); } return RecycleBin.uniqueInstance; } }
实现了Runnbale接口的Worker类:
package org.lyk.demo; import java.lang.Runnable; public class Worker implements Runnable { private RecycleBin bin; public Worker() {} public void run() { this.bin = RecycleBin.getInstance(); } public RecycleBin getRecycleBin() { return this.bin; } }
main方法代码:
package org.lyk.demo; //import java.util.Date; public class Hello { public static void main(String[] args) throws Exception { Worker worker1 = new Worker(); Worker worker2 = new Worker(); Thread t1 = new Thread(worker1); Thread t2 = new Thread(worker2); t1.start(); t2.start(); t1.join(); t2.join(); RecycleBin bin1 = worker1.getRecycleBin(); RecycleBin bin2 = worker2.getRecycleBin(); printInfo(bin1,bin2); } public static void printInfo(RecycleBin bin1, RecycleBin bin2) { System.out.println("bin1 == bin2 :" + (bin1==bin2)); System.out.println("bin1.hashCode() : " + bin1.hashCode()); System.out.println("bin2.hashCode() : " + bin2.hashCode()); } }
输出结果:
bin1 == bin2 :false
bin1.hashCode() : 31168322
bin2.hashCode() : 17225372
可见,在引入多线程后,如果没有对创建RecycleBin实例的代码做同步,那么会导致我们的单例模式失败。解决的方案大致有三种。
一) 在声明变量的时候直接初始化。这种做法简单粗暴,如果初始化一个单例对象很好资源的话不推荐这样做。因为只要类一加载,则该对象就会被创建。如果我们的代码中根本不用该实例,那么肯定会造成资源的浪费。
代码如下:
RecycleBin类
package org.lyk.demo; import java.lang.Thread; public class RecycleBin { public static RecycleBin uniqueInstance = new RecycleBin(); private RecycleBin() {} public static RecycleBin getInstance() { // if(null == RecycleBin.uniqueInstance) // { // try // { // Thread.sleep(3000); // }catch(InterruptedException e) // { // e.printStackTrace(); // } // RecycleBin.uniqueInstance = new RecycleBin(); // } return RecycleBin.uniqueInstance; } }
运行结果:
bin1 == bin2 :true
bin1.hashCode() : 31168322
bin2.hashCode() : 31168322
二) 使用同步方法。这样做的缺点是对方法加锁同样会降低程序运行效率。
RecycleBin代码如下:
package org.lyk.demo; import java.lang.Thread; public class RecycleBin { public static RecycleBin uniqueInstance ; private RecycleBin() {} public static synchronized RecycleBin getInstance() { if(null == RecycleBin.uniqueInstance) { try { Thread.sleep(3000); }catch(InterruptedException e) { e.printStackTrace(); } RecycleBin.uniqueInstance = new RecycleBin(); } return RecycleBin.uniqueInstance; } }
运行结果:
bin1 == bin2 :true
bin1.hashCode() : 31168322
bin2.hashCode() : 31168322
三)使用双重判断。这样做既保证了多线程的代码同步问题,又防止了每次调用函数都加锁导致的性能损耗。
RecycleBin代码如下:
package org.lyk.demo; import java.lang.Thread; public class RecycleBin { public static RecycleBin uniqueInstance ; private RecycleBin() {} public static RecycleBin getInstance() { if(null == RecycleBin.uniqueInstance) { synchronized(RecycleBin.class) { if(null == RecycleBin.uniqueInstance) { try { Thread.sleep(3000); }catch(InterruptedException e) { e.printStackTrace(); } RecycleBin.uniqueInstance = new RecycleBin(); } } } return RecycleBin.uniqueInstance; } }
运行结果如下:
bin1 == bin2 :true
bin1.hashCode() : 31168322
bin2.hashCode() : 31168322
单例设计模式无论是在开发中还是在我们找工作面试的时候都会经常遇到。所以单单的写出单例代码是不够的,面试的时候一定要将多线程同步的原理和方法讲出来,这样才是面试官真正想要的。
标签:
原文地址:http://www.cnblogs.com/kuillldan/p/5615237.html