标签:lis nal 私有化 div str new instance span 线程安全
饿汉式两种
懒汉式三种
双重检验
静态内部类
枚举
1 /** 2 * @author 79282 3 * 单列模式第一种 写法:饿汉式(静态常量) 4 */ 5 public class Singleton01 { 6 //私有化构造器 7 private Singleton01() { 8 9 } 10 11 //私有静态常量 12 private static final Singleton01 INSTANCE = new Singleton01(); 13 14 //get方法 15 public static Singleton01 getInstance() { 16 return INSTANCE; 17 } 18 19 } 20 class Test01{ 21 public static void main(String[] args) { 22 Singleton01 instance01 = Singleton01.getInstance(); 23 Singleton01 instance02 = Singleton01.getInstance(); 24 System.out.println(instance01 == instance02); 25 } 26 27 }
1 /** 2 * @author 79282 3 * 饿汉式 静态代码块 4 */ 5 public class Singleton02 { 6 //私有化构造器 7 private Singleton02() { 8 9 } 10 11 private static Singleton02 instance; 12 //静态代码块 13 static{ 14 instance = new Singleton02(); 15 } 16 17 public static Singleton02 getInstance() { 18 return instance; 19 } 20 } 21 class Test02{ 22 23 public static void main(String[] args) { 24 Singleton02 instance01 = Singleton02.getInstance(); 25 Singleton02 instance02 = Singleton02.getInstance(); 26 System.out.println(instance01 == instance02); 27 } 28 }
1 /** 2 * @author 79282 3 * 懒汉式 (线程不安全)不可用 4 */ 5 public class Singleton03 { 6 //私有化构造器 7 private Singleton03() { 8 9 } 10 11 private static Singleton03 instance = null; 12 13 14 public static Singleton03 getInstance() { 15 16 if (instance == null) { 17 instance = new Singleton03(); 18 } 19 return instance; 20 } 21 22 } 23 24 class Test03 { 25 public static void main(String[] args) { 26 Singleton03 instance01 = Singleton03.getInstance(); 27 Singleton03 instance02 = Singleton03.getInstance(); 28 System.out.println(instance01 == instance02); 29 } 30 }
1 /** 2 * @author 79282 3 * 懒汉式 线程安全的(使用同步方法) 不推荐使用 4 */ 5 public class Singleton04 { 6 //私有化构造器 7 private Singleton04() { 8 9 } 10 11 private static Singleton04 instance = null; 12 13 public static synchronized Singleton04 getInstance() { 14 if (instance == null) { 15 instance = new Singleton04(); 16 } 17 return instance; 18 } 19 } 20 class Test04{ 21 public static void main(String[] args) { 22 Singleton04 instance01 = Singleton04.getInstance(); 23 Singleton04 instance02 = Singleton04.getInstance(); 24 System.out.println(instance01 == instance02); 25 } 26 }
1 /** 2 * @author 79282 3 * 懒汉式 使用同步代码块 不可用 4 */ 5 public class Singleton05 { 6 private Singleton05() { 7 8 } 9 10 private static Singleton05 instance = null; 11 12 public static Singleton05 getInstance() { 13 if (instance == null) { 14 synchronized (Singleton05.class) { 15 instance = new Singleton05(); 16 } 17 } 18 return instance; 19 } 20 } 21 22 class Test05{ 23 public static void main(String[] args) { 24 25 Singleton05 instance01 = Singleton05.getInstance(); 26 Singleton05 instance02 = Singleton05.getInstance(); 27 System.out.println(instance01 == instance02); 28 29 } 30 }
1 /** 2 * @author 79282 3 * 懒汉式 双重检验(推荐使用) 4 */ 5 public class Singleton06 { 6 private Singleton06() { 7 8 } 9 10 private static Singleton06 instance = null; 11 public static Singleton06 getInstance() { 12 if (instance == null) { 13 synchronized (Singleton06.class) { 14 if (instance == null) { 15 instance = new Singleton06(); 16 } 17 18 19 } 20 } 21 22 return instance; 23 } 24 } 25 class Test06{ 26 public static void main(String[] args) { 27 Singleton06 instance01 = Singleton06.getInstance(); 28 Singleton06 instance02 = Singleton06.getInstance(); 29 System.out.println(instance01 == instance02); 30 } 31 }
1 /** 2 * @author 79282 3 * 静态内部类创建单例(推荐使用) 4 */ 5 public class Singleton07 { 6 private Singleton07() { 7 8 9 } 10 11 //私有静态内部类 12 private static class SingletonInstance{ 13 private static final Singleton07 INSTANCE = new Singleton07(); 14 } 15 16 public static Singleton07 getInstance() { 17 return SingletonInstance.INSTANCE; 18 } 19 20 21 } 22 class Test07{ 23 public static void main(String[] args) { 24 25 Singleton07 instance01 = Singleton07.getInstance(); 26 Singleton07 instance02 = Singleton07.getInstance(); 27 System.out.println(instance01 == instance02); 28 } 29 }
1 /** 2 * @author 79282 3 * 使用枚举 (推荐使用)/ 4 */ 5 public enum Singleton08 { 6 7 INSTANCE; 8 9 } 10 class Test08{ 11 public static void main(String[] args) { 12 Singleton08 instance01 = Singleton08.INSTANCE; 13 Singleton08 instance02 = Singleton08.INSTANCE; 14 System.out.println(instance01 == instance02); 15 } 16 17 }
标签:lis nal 私有化 div str new instance span 线程安全
原文地址:https://www.cnblogs.com/dreamjujujia/p/11695198.html