单例模式(Singleton Pattern)

来源:互联网 发布:狼群算法源代码 编辑:程序博客网 时间:2024/06/13 21:25

1、what is 单例模式?

答:设计的一个class只能创建一个实例对象,这样的做法叫做单例模式………………我好粗俗,但是个人理解就这么简单呀


2、实现方式

答:有多种方式,慢慢来吧,常用的………………


a、第一种,简单粗暴型,线程必须不安全,并发下直接卤蛋

public class SingleTest {private SingleTest() {super();System.out.println("SingleTest()");}private static SingleTest Instance = new SingleTest();public static SingleTest getInstance() {System.out.println("getInstance()");return Instance;}public static void main(String args[]) {SingleTest sTest = getInstance();}}


b、第二种,依然简单粗暴型,依然线程不安全(和第一种比,实例对象生成时机改变了,第一种类加载时就初始化实例对象,第二种你得调用静态方法时,才生成对象,而且我还作了判断,免得调用一次静态方法,就new一个实例对象,刺激吧)

public class SecSingle {private static SecSingle instance;private SecSingle(){super();}public static SecSingle getInstance() {if (instance == null) {instance = new SecSingle();}return instance;}}
 

c、第三种,没什么,a、b、c、三种全部是线程不安全的,如果多个线程对象同时访问此类的内存区域,那么并发下无法工作(每个线程都会拿到不同的实例对象)……,其实这三种就是类变量的初始化方法,换弹没换药

class ThirdSingleton {private static ThirdSingleton instance;private ThirdSingleton(){}static {instance = new ThirdSingleton();}public static ThirdSingleton getInstance(){return instance;}}


d、第四种,待哥哥来讲,算是a的变体,把静态变量撸成了final的而已,醉了,同样是线程不安全,N个线程一起加载该class,那就撸蛋了

class FourthSingleton {private final static FourthSingleton mInstance = new FourthSingleton();private FourthSingleton(){}public static FourthSingleton getInstance() {return mInstance;}}


e、第五种,线程安全的(不推荐使用),为静态方法加了一个同步锁(Class类对象锁),每个线程(实例对象)访问该静态方法时,都要等待上一个线程释放锁,效率比较低,因为当前class的实例对象,只需要初始化一次就可以

class FifthSingleton {private static FifthSingleton mInstance;private FifthSingleton(){super();}public static synchronized FifthSingleton getInstance(){if(mInstance == null){mInstance = new FifthSingleton();}return mInstance;}}

f、第六种,线程安全的(基本不能用),这次加个同步代码块,为啥不推荐使用,缺陷就在于如果有几个线程同时通过了if(mInstance == null),那就要醉了,每个线程都会生成当前class的实例对象了,效率bug

class SixthSingleton {private static SixthSingleton mInstance;private SixthSingleton(){super();}public static SixthSingleton getInstance(){if(mInstance == null) {synchronized (SixthSingleton.class) {mInstance = new SixthSingleton();}}return mInstance;}}

g、第七种、线程安全(推荐使用,效率相对高),传说中的双重检查null,醉了,弥补了第六种单例模式中的漏洞,爽,就算是3个线程进入if(mInstance == null),里面我又做了一次判断,也就是保证只会生成一个当前class的实例对象了。爽爽

class SeventhSingleton {private static SeventhSingleton mInstance;private SeventhSingleton(){}public static SeventhSingleton getInstance(){if(mInstance == null) {synchronized (SeventhSingleton.class) {if(mInstance == null) {mInstance = new SeventhSingleton();}}}return mInstance;}}


h、第八种,利用静态内部类(我还没用过呢),巧妙的利用静态成员的初始化,推荐用

class EighthSingleton {private EighthSingleton(){super();}private static class Single {private static final EighthSingleton INSTACE = new EighthSingleton();}public static EighthSingleton getInstance(){return Single.INSTACE;}}







0 0
原创粉丝点击