怎么使用Java单例模式
本篇内容主要讲解“怎么使用Java单例模式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用Java单例模式”吧!
成都创新互联公司从2013年创立,是专业互联网技术服务公司,拥有项目网站制作、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元内丘做网站,已为上家服务,为内丘各地企业和个人服务,联系电话:18982081108
1 饿汉式(静态变量)
package com.shi.design.singleton; /** * 单例模式:1 饿汉式(静态变量) * @author shiye * */ public class Singleton1 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 构造器私有化,外部不能new * 2. 本类内部创建对象实例 * 3. 提供一个共有的静态方法,返回实力对象 * @author shiye * */ class Singleton{ private Singleton(){} private static final Singleton instance = new Singleton(); public static Singleton getInstance() { return instance; } }
2 饿汉式(静态代码块)
package com.shi.design.singleton.type2; /** * 单例模式:2 饿汉式(静态代码块) * @author shiye * */ public class Singleton2 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 构造器私有化,外部不能new * 2. 再静态代码块中创建对象实例 * 3. 提供一个共有的静态方法,返回实力对象 * @author shiye * */ class Singleton{ private Singleton(){} private static Singleton instance; static { instance= new Singleton(); } public static Singleton getInstance() { return instance; } }
3 懒汉式式(线程不安全)
package com.shi.design.singleton.type3; /** * 单例模式:3 懒汉式式(线程不安全) * @author shiye * */ public class Singleton3 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 构造器私有化,外部不能new * 23. 提供一个共有的静态方法,再需要的时候去创建找个对象 * @author shiye * */ class Singleton{ private Singleton(){} private static Singleton instance; public static Singleton getInstance() { if(instance == null) { instance= new Singleton(); } return instance; } }
4 懒汉式式(线程安全)
package com.shi.design.singleton.type4; /** * 单例模式:4 懒汉式式(线程安全) * @author shiye * */ public class Singleton4 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 构造器私有化,外部不能new * 23. 提供一个共有的静态方法,再需要的时候去创建找个对象 (加锁synchronized 解决线程安全的问题) * @author shiye * */ class Singleton{ private Singleton(){} private static Singleton instance; public static synchronized Singleton getInstance() { if(instance == null) { instance= new Singleton(); } return instance; } }
5 懒汉式式(线程安全,双重检查 -推荐使用)
package com.shi.design.singleton.type5; /** * 单例模式:5 懒汉式式(线程安全,双重检查) * @author shiye * */ public class Singleton5 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 构造器私有化,外部不能new * 23. 提供一个共有的静态方法,再需要的时候去创建找个对象 * (加线程代码块synchronized 解决线程安全的问题,并且进行双重检查) * @author shiye * */ class Singleton{ private Singleton(){} //volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了。 private static volatile Singleton instance; public static Singleton getInstance() { if(instance == null) { synchronized (Singleton.class) { if(instance == null) { instance= new Singleton(); } } } return instance; } }
为什么要加 volatile 主要是为了禁止指令重排
6 懒加载式(静态内部类 推荐使用)
package com.shi.design.singleton.type6; /** * 单例模式:6 懒加载式(静态内部类) * @author shiye * */ public class Singleton6 { public static void main(String[] args) { Singleton singleton1 = Singleton.getInstance(); Singleton singleton2 = Singleton.getInstance(); System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); } } /** * 1. 构造器私有化,外部不能new * 2. 创建一个静态内部类(Singleton 类装载到内存中不会装载SingletonInstance类) * 3. 提供一个getInstance()方法: * 当getInstance()方法被调用的时候才回去实例化SingletonInstance类(装载类是线程安全的) * @author shiye * */ class Singleton{ private Singleton(){} public static class SingletonInstance{ public static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonInstance.INSTANCE; } }
7 使用枚举 (推荐使用)
package com.shi.design.singleton.type7; /** * 单例模式:7 使用枚举 * @author shiye * */ public class Singleton7 { public static void main(String[] args) { Singleton singleton1 = Singleton.INSTANCE; Singleton singleton2 = Singleton.INSTANCE; System.out.println(singleton1 == singleton2);//true System.out.println("singleton1.hashCode() = " + singleton1.hashCode()); System.out.println("singleton2.hashCode() = " + singleton2.hashCode()); singleton1.say(); singleton2.say(); } } /** * 使用枚举 * @author shiye * */ enum Singleton{ INSTANCE; public void say() { System.out.println("hello ~ "); } }
到此,相信大家对“怎么使用Java单例模式”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
当前题目:怎么使用Java单例模式
分享路径:http://cdiso.cn/article/poijsp.html