设计模式之单例模式

设计模式-单例模式

在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