JAVA设计模式之单例(singleton)

一、饿汉式

/**
* 饿汉式
*/
public class Singleton01 {

private static final Singleton01 instance = new Singleton01();

private Singleton01(){}

public static Singleton01 getInstance(){
return instance;
}

public static void main(String[] args) {
System.out.println(Singleton01.getInstance().hashCode());
System.out.println(Singleton01.getInstance().hashCode());
}
}

二、懒汉式

/**
 * 懒汉式
 */
public class Singleton02 {

    private static Singleton02 instance;

    private Singleton02(){}

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

    public static void main(String[] args) {
        System.out.println(Singleton02.getInstance().hashCode());
        System.out.println(Singleton02.getInstance().hashCode());
    }
}

三、懒汉式线程不安全测试

/**
* 懒汉式 线程不安全测试
*/
public class Singleton03 {

private static Singleton03 instance;
private static Object obj = new Object();

private Singleton03(){}

public static Singleton03 getInstance(){

if(instance == null){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton03();

}
return instance;
}

public static void main(String[] args) {
for(int i = 0; i< 100;i++) {
new Thread (() ->{
System.out.println(Singleton03.getInstance().hashCode());
}).start();
}
}
}

四、懒汉式线程安全写法(坑),并不能保证线程安全

/**
 * 懒汉式 线程不安全
 */
public class Singleton03 {

    private static Singleton03 instance;

    private Singleton03(){}

    public static Singleton03 getInstance(){

            if(instance == null){
                synchronized (Singleton03.class) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    instance = new Singleton03();
                }
            }
            return instance;
    }

    public static void main(String[] args) {
        for(int i = 0; i< 100;i++) {
            new Thread (() ->{
                System.out.println(Singleton03.getInstance().hashCode());
            }).start();
        }
    }
}

5、懒汉式线程安全写法DCL,必须添加volatile关键字防止指令重排序

/**
* 懒汉式 线程安全
*/
public class Singleton04 {

private static volatile Singleton04 instance;

private Singleton04(){}

public static Singleton04 getInstance(){

if(instance == null){
synchronized (Singleton04.class) {
if(instance == null){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton04();
}
}
}
return instance;
}

public static void main(String[] args) {
for(int i = 0; i< 100;i++) {
new Thread (() ->{
System.out.println(Singleton04.getInstance().hashCode());
}).start();
}
}
}

6、单例模式静态内部类实现

/**
 * 静态内部类实现 线程安全
 */
public class Singleton05 {

    private Singleton05(){}

    private static class SingletonHolder {
        private static final Singleton05 instance = new  Singleton05();
    }

    public static Singleton05 getInstance(){
        return SingletonHolder.instance;
    }

    public static void main(String[] args) {
        for(int i = 0; i< 100;i++) {
            new Thread (() ->{
                System.out.println(Singleton05.getInstance().hashCode());
            }).start();
        }
    }
}

7、终极实现之枚举类,Joshua Bloch 的《effective java》中推荐写法,防止反射和反序列化并且线程安全

/**
 * 枚举类实现 线程安全
 */
public enum Singleton06 {

    instance;
    public static void main(String[] args) {
        for(int i = 0; i< 100;i++) {
            new Thread (() ->{
                System.out.println(Singleton06.instance.hashCode());
            }).start();
        }
    }
}

 

posted @ 2020-04-25 13:08  引路的风  阅读(255)  评论(0编辑  收藏  举报