饿汉式(静态常量)应用实例 优缺点说明: 代码演示: 优缺点说明: 代码演示: 优缺点说明: 代码演示: 优缺点说明: 代码演示 优缺点说明: 代码演示: 优缺点说明: 代码演示 优缺点说明:单例设计模式八种方式
1、饿汉式(静态常量)
步骤如下:
public class SingletonTest01 { public static void main(String[] args) { //测试 Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance == instance2); // true System.out.println("instance.hashCode=" + instance.hashCode()); System.out.println("instance2.hashCode=" + instance2.hashCode()); } } //饿汉式(静态变量) class Singleton { //1. 构造器私有化, 外部能 new private Singleton() { } //2.本类内部创建对象实例 private final static Singleton instance = new Singleton(); //3. 提供一个公有的静态方法,返回实例对象 public static Singleton getInstance() { return instance; } }
会造成内存的浪费
多数都是调用 getInstance 方法,但是导致类装载的原因有很多种,因此不能确定有其他的方式(或者其他的静
态方法)导致类装载,这时候初始化 instance 就没有达到 lazy loading 的效果2、 饿汉式(静态代码块)
public class SingletonTest02 { public static void main(String[] args) { //测试 Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance == instance2); // true System.out.println("instance.hashCode=" + instance.hashCode()); System.out.println("instance2.hashCode=" + instance2.hashCode()); } } //饿汉式(静态变量) class Singleton { //1. 构造器私有化, 外部能 new private Singleton() { } //2.本类内部创建对象实例 private static Singleton instance; static { // 在静态代码块中,创建单例对象 instance = new Singleton(); } //3. 提供一个公有的静态方法,返回实例对象 public static Singleton getInstance() { return instance; } }
行静态代码块中的代码,初始化类的实例。优缺点和上面是一样的。3、懒汉式(线程不安全)
public class SingletonTest03 { public static void main(String[] args) { System.out.println("懒汉式 1 , 线程不安全~"); Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance == instance2); // true System.out.println("instance.hashCode=" + instance.hashCode()); System.out.println("instance2.hashCode=" + instance2.hashCode()); } } class Singleton { private static Singleton instance; private Singleton() {} //提供一个静态的公有方法,当使用到该方法时,才去创建 instance //即懒汉式 public static Singleton getInstance() { if(instance == null) { instance = new Singleton(); } return instance; } }
了这个判断语句,这时便会 产生多个实例。所以在多线程环境下不可使用这种方式4、 懒汉式(线程安全,同步方法)
public class SingletonTest04 { public static void main(String[] args) { System.out.println("懒汉式 2 , 线程安全~"); Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance == instance2); // true System.out.println("instance.hashCode=" + instance.hashCode()); System.out.println("instance2.hashCode=" + instance2.hashCode()); } } // 懒汉式(线程安全,同步方法) class Singleton { private static Singleton instance; private Singleton() {} //提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题 //即懒汉式 public static synchronized Singleton getInstance() { if(instance == null) { instance = new Singleton(); } return instance; } }
一次实例化代码就够了,后面的想获得该类实例,直接 return 就行了。 方法进行同步效率太低5、 懒汉式(线程安全,同步代码块)
不推荐使用6、 双重检查
public class SingletonTest06 { public static void main(String[] args) { System.out.println("双重检查"); Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance == instance2); // true System.out.println("instance.hashCode=" + instance.hashCode()); System.out.println("instance2.hashCode=" + instance2.hashCode()); } } // 懒汉式(线程安全,同步方法) class Singleton { private static volatile Singleton instance; private Singleton() {} //提供一个静态的公有方法,加入双重检查代码,解决线程安全问题, 同时解决懒加载问题 //同时保证了效率, 推荐使用 public static synchronized Singleton getInstance() { if(instance == null) { synchronized (Singleton.class) { if(instance == null) { instance = new Singleton(); } } } return instance; } }
样就可以保证线程安全了。
免的反复进行方法同步.7、 静态内部类
public class SingletonTest07 { public static void main(String[] args) { System.out.println("使用静态内部类完成单例模式"); Singleton instance = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance == instance2); // true System.out.println("instance.hashCode=" + instance.hashCode()); System.out.println("instance2.hashCode=" + instance2.hashCode()); } } // 静态内部类完成, 推荐使用 class Singleton { private static volatile Singleton instance; //构造器私有化 private Singleton() {} //写一个静态内部类,该类中有一个静态属性 Singleton private static class SingletonInstance { private static final Singleton INSTANCE = new Singleton(); } //提供一个静态的公有方法,直接返回 SingletonInstance.INSTANCE public static synchronized Singleton getInstance() { return SingletonInstance.INSTANCE; } }
会装载 SingletonInstance 类,从而完成 Singleton 的实例化。
初始化时,别的线程是无法进入的。8、 枚举
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~"); } }
新的对象。9、 单例模式在 JDK 应用的源码分析
1) 单例模式在 JDK 应用的源码分析
2) 单例模式注意事项和细节说明
用单例模式可以提高系统性能
对象),但又经常用到的对象、 工具类对象、频繁访问数据库或文件的对象(比如 数据源、session 工厂等)
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算