码迷,mamicode.com
首页 > 编程语言 > 详细

设计模式课程 设计模式精讲 8-2 单例设计模式-懒汉式及多线程Debug实战

时间:2019-08-16 21:04:19      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:测试   gem   nbsp   核心   with   local   sse   单线程   test   

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
com.geely.design.pattern.creational.singleton.LazySingleton@109d3f8b

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===com.geely.design.pattern.creational.singleton.LazySingleton@3eea8847
Disconnected from the target VM, address: ‘127.0.0.1:4375‘, transport: ‘socket‘
Thread-1===com.geely.design.pattern.creational.singleton.LazySingleton@3eea8847

Process finished with exit code 0

 

 

打印日志2:

 

 

Connected to the target VM, address: ‘127.0.0.1:4782‘, transport: ‘socket‘
结束了!!!
Thread-1===com.geely.design.pattern.creational.singleton.LazySingleton@28fa1b85
Thread-0===com.geely.design.pattern.creational.singleton.LazySingleton@57f4ea9d
Disconnected from the target VM, address: ‘127.0.0.1:4782‘, transport: ‘socket‘

Process finished with exit code 0

 

设计模式课程 设计模式精讲 8-2 单例设计模式-懒汉式及多线程Debug实战

标签:测试   gem   nbsp   核心   with   local   sse   单线程   test   

原文地址:https://www.cnblogs.com/1446358788-qq/p/11365927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!