1 主要内容
1.1 核心功能点
2 代码演练
2.1 单线程调用
2.2 多线程调用
1 主要内容
1.1 核心功能点
2 代码演练
2.1 单线程调用
测试类:
package com.geely.design.pattern.creational.singleton; public class Test { public static void main(String [] args){ //这样写异常,因为构造方法私有 // LazySingleton lazySingleton = new LazySingleton(); LazySingleton lazySingleton = LazySingleton.getInstance(); System.out.println(lazySingleton); } }
实体类:
package com.geely.design.pattern.creational.singleton; public class LazySingleton { /* 属性私有,其他外部类,无法调用该属性,安全 */ private static LazySingleton lazySingleton = null; /** * 构造方法私有,其他类无法实例化该类 */ private LazySingleton(){ } /** * 这里写静态方法:因为外部类无法实例化创建出该类, * 只能通过该类的静态方法获取到该类。 * @return */ public static LazySingleton getInstance(){ if(lazySingleton == null){ lazySingleton = new LazySingleton(); } return lazySingleton; } }
打印结果:
"C:\Program Files\Java\jdk1.7.0_79\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=3988:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\design_pattern\target\classes" com.geely.design.pattern.creational.singleton.Test [email protected]3f8b Process finished with exit code 0
2.2 多线程调用
测试类:
package com.geely.design.pattern.creational.singleton; public class Test { /*public static void main(String [] args){ //这样写异常,因为构造方法私有 // LazySingleton lazySingleton = new LazySingleton(); LazySingleton lazySingleton = LazySingleton.getInstance(); System.out.println(lazySingleton); }*/ public static void main(String [] args){ Thread thread1 = new Thread(new T()); Thread thread2 = new Thread(new T()); thread1.start(); thread2.start(); System.out.println("结束了!!!"); } }
线程类:
package com.geely.design.pattern.creational.singleton; /** * 注:该类为线程类,调用LazySingleton */ public class T implements Runnable{ @Override public void run() { LazySingleton lazySingleton = LazySingleton.getInstance(); System.out.println(Thread.currentThread().getName()+"==="+lazySingleton); } }
实体类:
package com.geely.design.pattern.creational.singleton; public class LazySingleton { /* 属性私有,其他外部类,无法调用该属性,安全 */ private static LazySingleton lazySingleton = null; /** * 构造方法私有,其他类无法实例化该类 */ private LazySingleton(){ } /** * 这里写静态方法:因为外部类无法实例化创建出该类, * 只能通过该类的静态方法获取到该类。 * @return */ public static LazySingleton getInstance(){ if(lazySingleton == null){ lazySingleton = new LazySingleton(); } return lazySingleton; } }
打印日志1:
"C:\Program Files\Java\jdk1.7.0_79\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:4375,suspend=y,server=n -javaagent:C:\Users\weijingli\.IdeaIC2018.1\system\captureAgent\debugger-agent.jar=file:/C:/Users/weijingli/AppData/Local/Temp/capture.props -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\design_pattern\target\classes;D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar" com.geely.design.pattern.creational.singleton.Test Connected to the target VM, address: ‘127.0.0.1:4375‘, transport: ‘socket‘ 结束了!!! Thread-0===[email protected]8847 Disconnected from the target VM, address: ‘127.0.0.1:4375‘, transport: ‘socket‘ Thread-1===[email protected]8847 Process finished with exit code 0
打印日志2:
Connected to the target VM, address: ‘127.0.0.1:4782‘, transport: ‘socket‘ 结束了!!! Thread-[email protected]28fa1b85 Thread-0===[email protected]ea9d Disconnected from the target VM, address: ‘127.0.0.1:4782‘, transport: ‘socket‘ Process finished with exit code 0
原文地址:https://www.cnblogs.com/1446358788-qq/p/11365927.html
时间: 2024-11-06 09:42:23