线程安全的两种单例模式

两种常见的单例模式

静态内部类单例模式

/*
 *  静态内部类单例模式
 */
public class Singleton {
    private Singleton() {
    }

    private static final class SingletonHolder {
        private static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println(Singleton.getInstance().hashCode());
            }

        }, "t1");

        Thread t2 = new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println(Singleton.getInstance().hashCode());
            }

        }, "t2");

        Thread t3 = new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println(Singleton.getInstance().hashCode());
            }

        }, "t3");

        t1.start();
        t2.start();
        t3.start();
    }
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class InnerSingleton {
    private InnerSingleton() {
    }

    private static final class InnerSingletonHandle {
        private static final InnerSingleton INSTANCE = new InnerSingleton();
    }

    public static InnerSingleton getInstance() {
        return InnerSingletonHandle.INSTANCE;
    }

    public static void main(String[] args) {
        int num = 10;
        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < num; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + ":\t" + InnerSingleton.getInstance().hashCode());
                }
            });
        }
        executorService.shutdown();
    }
}

运行结果

pool-1-thread-1:    991873903
pool-1-thread-2:    991873903
pool-1-thread-3:    991873903
pool-1-thread-4:    991873903
pool-1-thread-6:    991873903
pool-1-thread-7:    991873903
pool-1-thread-8:    991873903
pool-1-thread-10:   991873903
pool-1-thread-5:    991873903
pool-1-thread-9:    991873903

Process finished with exit code 0

双重校验单例模式

/*
 * 双重校验单例模式
 */
public class Singleton {
    private static volatile Singleton instance;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
                // instance = new Singleton();
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println(Singleton.getInstance().hashCode());
            }

        }, "t1");

        Thread t2 = new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println(Singleton.getInstance().hashCode());
            }

        }, "t2");

        Thread t3 = new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println(Singleton.getInstance().hashCode());
            }

        }, "t3");

        t1.start();
        t2.start();
        t3.start();
    }
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DoubleCheckSingleton {
    private DoubleCheckSingleton() {
    }

    private static volatile DoubleCheckSingleton INSTANCE;

    public static DoubleCheckSingleton getInstance() {
        if (INSTANCE == null) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (DoubleCheckSingleton.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DoubleCheckSingleton();
                }
                // INSTANCE = new DoubleCheckSingleton();
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        int num = 10;
        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < num; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + ":\t" + DoubleCheckSingleton.getInstance().hashCode());
                }
            });
        }
        executorService.shutdown();
    }
}

运行结果

pool-1-thread-1:    405652733
pool-1-thread-4:    405652733
pool-1-thread-7:    405652733
pool-1-thread-2:    405652733
pool-1-thread-6:    405652733
pool-1-thread-5:    405652733
pool-1-thread-3:    405652733
pool-1-thread-8:    405652733
pool-1-thread-9:    405652733
pool-1-thread-10:   405652733

Process finished with exit code 0

最后总结

1306719-20181107155022590-664214116.png
1306719-20181107155028084-1715481635.png

转载于:https://www.cnblogs.com/hglibin/p/9397974.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值