package com.atguigu.singleton.type1; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class SingleTest01 { public static void main(String[] args) { //测试 Singleton singleton = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton == singleton2); System.out.println(singleton.hashCode()); System.out.println(singleton2.hashCode()); } } //饿汉式(静态变量) class Singleton { //1.构造器私有化 private Singleton() { } //2.在本类内部创建对象实例 类加载一次 private final static Singleton instance = new Singleton(); //3.对外提供一个公有的静态方法,返回实例对象 public static Singleton getInstance() { return instance; } }
---------------------------------------------------------------------------------------------------------------------------------
package com.atguigu.singleton.type2; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class SingleTest02 { public static void main(String[] args) { //测试 Singleton singleton = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton == singleton2); System.out.println(singleton.hashCode()); System.out.println(singleton2.hashCode()); } } //饿汉式(静态变量) class Singleton { //1.构造器私有化 private Singleton() { } static {//在静态代码块中,创建单例对象 instance = new Singleton(); } //2.在本类内部创建对象实例 类加载一次 private static Singleton instance; //3.对外提供一个公有的静态方法,返回实例对象 public static Singleton getInstance() { return instance; } }
---------------------------------------------------------------------------------------------------------------------------------
package com.atguigu.singleton.type3; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class SingletonTest03 { public static void main(String[] args) { //测试 Singleton singleton = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton == singleton2); System.out.println(singleton.hashCode()); System.out.println(singleton2.hashCode()); } } class Singleton { private static Singleton instance; private Singleton() { } //提供一个静态的公有方法,当使用到该方法时,才去创建 instance //即懒汉式 需要注意线程安全 //如果在多线程情况下,一个线程进入了if(singleton == null)判断语句块,还未来得及 //往下执行,另一个线程也可能通过了这个判断语句,这时便会产生多个实例, //所以在多线程情况下不可使用这种方式,要注意线程安全 public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
---------------------------------------------------------------------------------------------------------------------------------
package com.atguigu.singleton.type4; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class SingletonTest04 { public static void main(String[] args) { //测试 Singleton singleton = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton == singleton2); System.out.println(singleton.hashCode()); System.out.println(singleton2.hashCode()); } } class Singleton { private static Singleton instance; private Singleton() { } //提供一个静态的公有方法,当使用到该方法时,才去创建 instance //即懒汉式 需要注意线程安全 //如果在多线程情况下,一个线程进入了if(singleton == null)判断语句块,还未来得及 //往下执行,另一个线程也可能通过了这个判断语句,这时便会产生多个实例, //所以在多线程情况下不可使用这种方式,要注意线程安全 //加入了同步处理的代码,解决线程安全 //效率太低,获取对象时,每一次都需要进行同步 public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
---------------------------------------------------------------------------------------------------------------------------------
package com.atguigu.singleton.type5; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class SingletonTest05 { public static void main(String[] args) { //测试 Singleton singleton = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton == singleton2); System.out.println(singleton.hashCode()); System.out.println(singleton2.hashCode()); } } class Singleton { private static Singleton instance; private Singleton() { } public static Singleton getInstance() { if (instance == null) {//不能解决线程安全 synchronized (Singleton.class) { instance = new Singleton(); } } return instance; } }
---------------------------------------------------------------------------------------------------------------------------------
package com.atguigu.singleton.type6; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class SingletonTest06 { public static void main(String[] args) { } } //双重检查 解决了懒加载与效率的问题 推荐使用 class Singleton { private static volatile Singleton singleton; private Singleton() {} public static Singleton getInstance() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; } }
---------------------------------------------------------------------------------------------------------------------------------
package com.atguigu.singleton.type7; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class SingletonTest07 { public static void main(String[] args) { //测试 Singleton singleton = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton == singleton2); System.out.println(singleton.hashCode()); System.out.println(singleton2.hashCode()); } } //静态内部类完成,推荐使用 class Singleton { private Singleton() { } private static class SingletonInstance { private static final Singleton INSTANCE = new Singleton(); } public static synchronized Singleton getInstance() { return SingletonInstance.INSTANCE; } }
---------------------------------------------------------------------------------------------------------------------------------
package com.atguigu.singleton.type8; /** * @author 魏麒羽 * @version 1.0 * 我亦无他,唯手熟尔! */ public class SingletonTest08 { public static void main(String[] args) { //测试 Singleton instance = Singleton.INSTANCE; Singleton instance2 = Singleton.INSTANCE; System.out.println(instance == instance2); System.out.println(instance.hashCode()); System.out.println(instance2.hashCode()); instance.sayOk(); } } //枚举完成 enum Singleton { INSTANCE; public void sayOk() { System.out.println("ok~"); } } //源码单例模式的饿汉式 class Runtime { private static Runtime currentRuntime = new Runtime(); /** * Returns the runtime object associated with the current Java application. * Most of the methods of class <code>Runtime</code> are instance * methods and must be invoked with respect to the current runtime object. * * @return the <code>Runtime</code> object associated with the current * Java application. */ public static Runtime getRuntime() { return currentRuntime; } /** * Don't let anyone else instantiate this class */ private Runtime() { } }