标签:Singleton getInstance HungarySingleton 模式 static 单例 public
1、单例模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点。
2、代码示例:
2.1、饿汉模式:
package com.aibaiyang.idemo.service.singleton; /** * @Author zhong guo * @Date 2019/8/31 15:01 * @description 饿汉单例模式(多线程环境依然是单例) **/ public class HungarySingleton { private static HungarySingleton instance = new HungarySingleton(); private HungarySingleton(){ } public static HungarySingleton getInstance(){ return instance; } }View Code
2.2、懒汉模式:
package com.aibaiyang.idemo.service.singleton; /** * @Author zhong guo * @Date 2019/8/31 11:43 * @description 懒汉单例模式 **/ public class Singleton { private volatile static Singleton instance; /** * 构造方法让其private,这就堵死了外面利用new关键字 * 创建此类示例的可能 */ private Singleton(){ } /** * 此方法为获得此类示例的唯一访问点 * @return */ public static Singleton getInstance(){ if(instance == null){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (Singleton.class){ if(instance == null){ instance = new Singleton(); } } } return instance; } }View Code
2.3、多线程类
HungaryThread类
package com.aibaiyang.idemo.service.singleton; /** * @Author zhong guo * @Date 2019/8/31 15:13 * @description 饿汉线程 **/ public class HungaryThread extends Thread{ public void run(){ //打印当前对象的hash值,相同则代表同一个对象,不同代表不同对象。 System.out.println("当前对象hashCode:" + HungarySingleton.getInstance().hashCode()); } }View Code
LazyThread类
package com.aibaiyang.idemo.service.singleton; /** * @Author zhong guo * @Date 2019/8/31 11:55 * @description 懒汉线程 **/ public class LazyThread implements Runnable{ @Override public void run() { //打印当前对象的hash值,相同则代表同一个对象,不同代表不同对象。 System.out.println("当前对象hashCode:" + Singleton.getInstance().hashCode()); } }View Code
2.3、单例模式客户端:
SingletonClient类
package com.aibaiyang.idemo.service.singleton; /** * @Author zhong guo * @Date 2019/8/31 11:50 * @description 单例模式客户端 **/ public class SingletonClient { public static void main(String[] args){ //1、懒汉单例实例 lazySingletonDemo(); //2、饿汉单例实例 hungarySingletonDemo(); } static void lazySingletonDemo(){ //实例1 Singleton singleton1 = Singleton.getInstance(); //实例2 Singleton singleton2 = Singleton.getInstance(); if(singleton1 == singleton2){ System.out.println("懒汉相同的实例!"); }else { System.out.println("懒汉不同的实例"); } } static void hungarySingletonDemo(){ HungarySingleton hungarySingleton1 = HungarySingleton.getInstance(); HungarySingleton hungarySingleton2 = HungarySingleton.getInstance(); if(hungarySingleton1 == hungarySingleton2){ System.out.println("饿汉相同的实例!"); }else { System.out.println("饿汉不同的实例!"); } } }View Code
SingletonTheadClient类
package com.aibaiyang.idemo.service.singleton; /** * @Author zhong guo * @Date 2019/8/31 12:04 * @description 单例模式多线程客户端 **/ public class SingletonTheadClient { public static void main(String[] args) { //1、懒汉多线程单例测试 lazySingletonTest(); //2、饿汉多线程单例测试 hungarySingletonTest(); } static void lazySingletonTest(){ LazyThread myThread = new LazyThread(); //线程1 Thread thread1 = new Thread(myThread); //线程2 Thread thread2 = new Thread(myThread); thread1.start(); thread2.start(); } static void hungarySingletonTest(){ //线程1 HungaryThread hungaryThread1 = new HungaryThread(); //线程2 HungaryThread hungaryThread2 = new HungaryThread(); hungaryThread1.start(); hungaryThread2.start(); } }View Code
3、github:
标签:Singleton,getInstance,HungarySingleton,模式,static,单例,public 来源: https://www.cnblogs.com/aibaiyang/p/11443162.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。