23种设计模式之单例模式(8种写法)

``23种设计模式之单例模式(8种写法)

/**
 * 饿汉式
 * 

* 高效 没有线程安全问题 简单 *

* jvm保证线程安全问题 */ public class Merge01 { private static final Merge01 test = new Merge01(); private Merge01() { } public static Merge01 getInstance() { return test; } public static void main(String[] args) { Merge01 test001 = Merge01.getInstance(); Merge01 test002 = Merge01.getInstance(); System.out.println(test002 == test002); } } /** * 用静态块初始化 */ public class Merge02 { private static final Merge02 INSTANCE; static { INSTANCE = new Merge02(); } private Merge02() { } ; public static Merge02 getInstance() { return INSTANCE; } ; public static void main(String[] args) { Merge02 test001 = Merge02.getInstance(); Merge02 test002 = Merge02.getInstance(); System.out.println(test002 == test002); } } /** * 懒汉式 * 懒加载模式 lazy-loading * 做到了按需初始化,但是多线程访问时有线程安全问题 */ public class Merge03 { private static Merge03 INSTANCE; private Merge03() { } ; public static Merge03 getInstance() { if (INSTANCE == null) { try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } INSTANCE = new Merge03(); } return INSTANCE; } public void m() { System.out.println("m"); } public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> { System.out.println(Merge03.getInstance().hashCode()); }).start(); } } } /** * 懒加载模式 lazy-loading 加锁避免了线程安全问题,但是效率受影响 */ public class Merge04 { private static Merge04 INSTANCE; private Merge04() { } ; public static synchronized Merge04 getInstance() { if (INSTANCE == null) { try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } INSTANCE = new Merge04(); } return INSTANCE; } public void m() { System.out.println("m"); } public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> { System.out.println(Merge04.getInstance().hashCode()); }).start(); } } } /** * 懒加载模式 lazy-loading * instance == null 和sychnorized方法没有同步,线程安全问题存在、 */ public class Merge05 { private static Merge05 INSTANCE; private Merge05() { } ; public static Merge05 getInstance() { if (INSTANCE == null) { synchronized (Merge05.class) { try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } INSTANCE = new Merge05(); } } return INSTANCE; } public void m() { System.out.println("m"); } public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> { System.out.println(Merge05.getInstance().hashCode()); }).start(); } } } /** * 懒加载模式 lazy-loading * 双重检查,接近完美 */ public class Merge06 { private static Merge06 INSTANCE; private Merge06() { } ; public static Merge06 getInstance() { if (INSTANCE == null) { synchronized (Merge06.class) { if (INSTANCE == null) { try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } INSTANCE = new Merge06(); } } } return INSTANCE; } public void m() { System.out.println("m"); } public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> { System.out.println(Merge06.getInstance().hashCode()); }).start(); } } } /** * 懒加载模式 lazy-loading *

* 静态内部类方式,jvm保证线程安全问题, *

* Merge07Holder在调用getInstance()时才被实例一次,所以INSTANCE实例也只被实例一次,几乎完美的方式 */ public class Merge07 { private Merge07() { } ; private static class Merge07Holder { private static final Merge07 INSTANCE = new Merge07(); } public static Merge07 getInstance() { return Merge07Holder.INSTANCE; } public void m() { System.out.println("m"); } public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> { System.out.println(Merge07.getInstance().hashCode()); }).start(); } } } /** * 枚举类写法 * 解决线程安全问题,同时方式反序列化 */ public enum Merge08 { INSTANCE; public void m() { } public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> { System.out.println(Merge08.INSTANCE.hashCode()); }).start(); } } }

你可能感兴趣的:(java,java,设计模式)