java单例模式实现方式

/**
 * 线程安全,类加载时实例就会被初始化
 */
public class Singleton01 {
    private static int threadNum = 10;
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
    private static final Singleton01 singleton = new Singleton01();

    private Singleton01(){

    }

    public static Singleton01 getInstance(){
        return singleton;
    }

    public static void main(String[] args) throws InterruptedException {
        final Set<Singleton01> set = Collections.synchronizedSet(new HashSet<Singleton01>());
        for(int index=0;index<threadNum;index++){
            new Thread(new Runnable() {
                public void run() {
                    try {
                        //所有线程都在此等候
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    set.add(Singleton01.getInstance());
                    countDownLatch2.countDown();
                }
            }).start();
        }
        //所有线程都启动后,再放行
        countDownLatch.countDown();
        //等候所有线程执行完add操作
        countDownLatch2.await();
        System.out.println(set.size());
    }
}
/**
 * 线程安全,类加载时实例就会被初始化
 */
public class Singleton02 {
    private static int threadNum = 1000;
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
    private static final Singleton02 singleton;

    static {
        singleton = new Singleton02();
    }

    private Singleton02(){

    }

    public static Singleton02 getInstance(){
        return singleton;
    }

    public static void main(String[] args) throws InterruptedException {
        final Set<Singleton02> set = Collections.synchronizedSet(new HashSet<Singleton02>());
        for(int index=0;index<threadNum;index++){
            new Thread(new Runnable() {
                public void run() {
                    try {
                        //所有线程都在此等候
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    set.add(Singleton02.getInstance());
                    countDownLatch2.countDown();
                }
            }).start();
        }
        //所有线程都启动后,再放行
        countDownLatch.countDown();
        //等候所有线程执行完add操作
        countDownLatch2.await();
        System.out.println(set.size());
    }
}

/**
 *非线程安全
 */
public class Singleton03 {
    private static int threadNum = 5000;
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
    private static Singleton03 singleton;


    private Singleton03(){

    }

    public static Singleton03 getInstance(){
        if(singleton == null){
            singleton = new Singleton03();
        }
        return singleton;
    }

    public static void main(String[] args) throws InterruptedException {
        final Set<Singleton03> set = Collections.synchronizedSet(new HashSet<Singleton03>());
        for(int index=0;index<threadNum;index++){
            new Thread(new Runnable() {
                public void run() {
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    set.add(Singleton03.getInstance());
                    countDownLatch2.countDown();
                }
            }).start();
        }
        countDownLatch.countDown();
        countDownLatch2.await();
        System.out.println(set.size());
    }
}
/**
 *线程安全,效率低
 */
public class Singleton04 {
    private static int threadNum = 5000;
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
    private static volatile Singleton04 singleton;


    private Singleton04(){

    }

    @Override
    public int hashCode() {
        return 1;
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    public static synchronized Singleton04 getInstance(){
        if(singleton == null){
            singleton = new Singleton04();
            System.out.println("构造方法被执行!!!");
        }
        return singleton;
    }

    public static void main(String[] args) throws InterruptedException {
        final Set<Singleton04> set = Collections.synchronizedSet(new HashSet<Singleton04>());
        for(int index=0;index<threadNum;index++){
            new Thread(new Runnable() {
                public void run() {
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    set.add(Singleton04.getInstance());
                    countDownLatch2.countDown();
                }
            }).start();
        }
        countDownLatch.countDown();
        countDownLatch2.await();
        System.out.println(set.size());
    }
}

/**
 *非线程安全
 */
public class Singleton05 {
    private static int threadNum = 5000;
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
    private static volatile Singleton05 singleton;


    private Singleton05(){

    }

    public static Singleton05 getInstance(){
        if(singleton == null){
            synchronized(Singleton05.class){
                singleton = new Singleton05();
            }
        }
        return singleton;
    }

    public static void main(String[] args) throws InterruptedException {
        final Set<Singleton05> set = Collections.synchronizedSet(new HashSet<Singleton05>());
        for(int index=0;index<threadNum;index++){
            new Thread(new Runnable() {
                public void run() {
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    set.add(Singleton05.getInstance());
                    countDownLatch2.countDown();
                }
            }).start();
        }
        countDownLatch.countDown();
        countDownLatch2.await();
        System.out.println(set.size());
    }
}

/**
 *双检测,线程安全、懒加载
 */
public class Singleton06 {
    private static int threadNum = 5000;
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);
    private static Singleton06 singleton;


    private Singleton06(){

    }

    public static Singleton06 getInstance(){
        if(singleton == null){
            synchronized(Singleton06.class){
                if(singleton == null){
                    singleton = new Singleton06();
                }
            }
        }
        return singleton;
    }

    public static void main(String[] args) throws InterruptedException {
        final Set<Singleton06> set = Collections.synchronizedSet(new HashSet<Singleton06>());
        for(int index=0;index<threadNum;index++){
            new Thread(new Runnable() {
                public void run() {
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    set.add(Singleton06.getInstance());
                    countDownLatch2.countDown();
                }
            }).start();
        }
        countDownLatch.countDown();
        countDownLatch2.await();
        System.out.println(set.size());
    }
}

/**
 *静态内部类,线程安全、懒加载
 */
public class Singleton07 {
    private static int threadNum = 5000;
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static CountDownLatch countDownLatch2 = new CountDownLatch(threadNum);


    private Singleton07(){

    }

    static class SingletonHolder{
        static final Singleton07 singleton = new Singleton07();
    }

    public static Singleton07 getInstance(){
        return SingletonHolder.singleton;
    }

    public static void main(String[] args) throws InterruptedException {
        final Set<Singleton07> set = Collections.synchronizedSet(new HashSet<Singleton07>());
        for(int index=0;index<threadNum;index++){
            new Thread(new Runnable() {
                public void run() {
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    set.add(Singleton07.getInstance());
                    countDownLatch2.countDown();
                }
            }).start();
        }
        Thread.sleep(1000);
        countDownLatch.countDown();
        countDownLatch2.await();
        System.out.println(set.size());
    }
}

/**
 *枚举,线程安全、防止反序列化(怪怪的)
 */
public enum Singleton08 {
    INSTANCE;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值