JAVA設計模式之單例(singleton)

1、餓漢式java

/**
* 餓漢式
*/
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());
}
}

2、懶漢式安全

/**
 * 懶漢式
 */
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());
    }
}

3、懶漢式線程不安全測試測試

/**
* 懶漢式 線程不安全測試
*/
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();
}
}
}

4、懶漢式線程安全寫法(坑),並不能保證線程安全spa

/**
 * 懶漢式 線程不安全
 */
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();
        }
    }
}

五、懶漢式線程安全寫法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();
}
}
}

六、單例模式靜態內部類實現code

/**
 * 靜態內部類實現 線程安全
 */
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();
        }
    }
}

七、終極實現之枚舉類,Joshua Bloch 的《effective java》中推薦寫法,防止反射和反序列化而且線程安全blog

/**
 * 枚舉類實現 線程安全
 */
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();
        }
    }
}
相關文章
相關標籤/搜索