首页 > 其他 > 详细

设计模式之单例模式

时间:2018-09-22 00:24:10      阅读:212      评论:0      收藏:0      [点我收藏+]

设计模式-单例模式

在Java设计模式中,单例模式相对来说算是比较简单的一种构建模式。适用的场景在于:对于定义的一个类,在整个应用程序执行期间只有唯一的一个实例对象。

主要实现方式包括饿汉式、懒汉式;懒汉式需要注意线程安全问题。
核心是理解synchronize和volatile关键字。

/**
 * 饿汉模式
 */
public class Singleton {
    private static volatile Singleton instance = new Singleton();

    private Singleton() {
    }

    public static Singleton getInstance() {
        return instance;
    }
}

/**
 * 饿汉模式测试
 */
public class Test implements Runnable {
    @Override
    public void run() {
        Singleton singleton = Singleton.getInstance();
        System.out.println(Thread.currentThread().getName() + "---" + singleton.hashCode());
    }

    public static void main(String[] args) {
        Test test = new Test();
        new Thread(test).start();
        new Thread(test).start();
        new Thread(test).start();
        new Thread(test).start();
    }
}

Thread-0---1174385069
Thread-1---1174385069
Thread-2---1174385069
Thread-3---1174385069

Process finished with exit code 0
/**
 * 懒汉模式
 */
public class Singleton {
    private static Singleton instance;

    private Singleton() {
    }
    
    public static Singleton getInstance() {
        if (instance == null)
            instance = new Singleton();
        return instance;
    }
}

/**
 * 懒汉模式测试
 */
public class Test implements Runnable {
    @Override
    public void run() {
        Singleton singleton = Singleton.getInstance();
        System.out.println(Thread.currentThread().getName() + "---" + singleton.hashCode());
    }

    public static void main(String[] args) {
        Test test = new Test();
        new Thread(test).start();
        new Thread(test).start();
        new Thread(test).start();
        new Thread(test).start();
    }
}
线程不安全,可能产生多个实例
Thread-2---1549208166
Thread-0---1272861279
Thread-1---1549208166
Thread-3---1272861279

Process finished with exit code 0
/**
 * 懒汉模式 线程安全,instance需要volatile关键字修饰,并且双重验证+同
 * 步锁才能保证线程安全
 */
public class Singleton {
    private static volatile Singleton instance;//volatile保证可见性

    private Singleton() {
    }

    public static Singleton getInstance() {
        /**
         * 双重验证加同步锁
         */
         if(instance == null){
            synchronized (Singleton.class) {
            if (instance == null)
                instance = new Singleton();
            }
         }
        return instance;
    }
}

/**
 * 懒汉模式 线程安全 测试
 */
public class Test implements Runnable {
    @Override
    public void run() {
        Singleton singleton = Singleton.getInstance();
        System.out.println(Thread.currentThread().getName() + "---" + singleton.hashCode());
    }

    public static void main(String[] args) {
        Test test = new Test();
        new Thread(test).start();
        new Thread(test).start();
        new Thread(test).start();
        new Thread(test).start();
    }
}
Thread-1---33689776
Thread-0---33689776
Thread-2---33689776
Thread-3---33689776

Process finished with exit code 0

设计模式之单例模式

原文:https://www.cnblogs.com/icelan/p/9683034.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!