Java单例模式结合JUC原子类爆发的新想法

单例模式是23种设计模式中的一种,关于它的介绍,已经数不胜数了,一般网上的实现方式无非是什么懒汉式,饿汉式,内部类,枚举,volatile加双重校验锁等等,今天我又想到一个新的实现方式,利用J.U.C提供的原子类,代码如下:


class SingleTon {
    private static AtomicReference<SingleTon> reference = new AtomicReference<>();

    private SingleTon() {
    }

    public static SingleTon getInstance() {
        while (reference.get() == null) {
            reference.compareAndSet(null, new SingleTon());
        }
        return reference.get();
    }
}

测试:
1. 测试线程类

class TestThread extends Thread {
    HashSet<SingleTon> hashSet;
    boolean flag;
    int number;

    public TestThread(HashSet<SingleTon> hashSet, boolean flag, int number) {
        this.hashSet = hashSet;
        this.flag = flag;
        this.number = number;
    }

    @Override
    public void run() {
        if (flag) {
            TestThread[] threads = new TestThread[number];
            for (int i = 0; i < number; i++) {
                threads[i] = new TestThread(hashSet, !flag, 0);
            }
            for (int i = 0; i < number; i++) {
                threads[i].start();
            }
        }
        hashSet.add(SingleTon.getInstance());
        System.out.println("当前hashset大小:" + hashSet.size());
    }
}
  1. 测试实例
HashSet<SingleTon> hashSet = new HashSet<>();

        int number = 50;

        TestThread[] threads = new TestThread[number];
        for (int i = 0; i < number; i++) { threads[i] = new TestThread(hashSet, true, 200); }
        for (int i = 0; i < number; i++) { threads[i].start(); }   

然而结果并不明朗,某些情况下,打印出的结果为:

2
2
2
2
2
2
2
2
2
2
2
2
2
2

瞬间忧伤了,本来以为发现新大陆了的,结果,哎!不死心,是什么问题呢?为什么会有两个Singleton呢?
ok,修改测试方法:

HashSet<SingleTon> hashSet = new HashSet<>();

        int number = 50;

        TestThread[] threads = new TestThread[number];
        for (int i = 0; i < number; i++) {
            threads[i] = new TestThread(hashSet, true, 200);
        }
        for (int i = 0; i < number; i++) {
            threads[i].start();
        }
        Thread.sleep(3000);
        if (hashSet.size() > 1) {
            for (SingleTon singleTon : hashSet) {
                System.out.println(singleTon + "" + singleTon.hashCode());
            }
        }

虽然结果还是有2,3,1,但是,最后的打印信息显示,HashSet中只有一个Singleton实例。这是为什么呢?因为HashSet是非线程安全的,在多线程环境下,容易发生状态不一致的情况。我们换成CopyOnWriteArraySet再来试一下。

class TestThread extends Thread {
    CopyOnWriteArraySet<SingleTon> hashSet;
    boolean flag;
    int number;

    public TestThread(CopyOnWriteArraySet<SingleTon> hashSet, boolean flag,
            int number) {
        this.hashSet = hashSet;
        this.flag = flag;
        this.number = number;
    }

    @Override
    public void run() {
        if (flag) {
            TestThread[] threads = new TestThread[number];
            for (int i = 0; i < number; i++) {
                threads[i] = new TestThread(hashSet, !flag, 0);
            }
            for (int i = 0; i < number; i++) {
                threads[i].start();
            }
        }
        hashSet.add(SingleTon.getInstance());
        System.out.println(hashSet.size());
    }
}

OK,一切正常。
欢迎大家指出不对之处。

    原文作者:JUC
    原文地址: https://blog.csdn.net/u013769320/article/details/47809423
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞