idea多线程debug技巧
2019-02-17 本文已影响0人
Albert_Yu
前言
本文主要针对对开发中使用IDEA开发工具对debug技巧有一定了解的童鞋
想学习和了解相关idea debug技巧的童鞋可查看我之前写过的一篇文章idea新手使用教程小结
idea多线程debug设置

如上图所示将debug线程的挂起方式设置为Thread之后点击Done,当然在这里你也可以点击 Make Default,这样在之后的断点都会默认选择挂起方式选择的就是Thread
All:只会debug到本线程的断点
idea多线程debug案例演示
这里用一个非线程安全的懒汉式单例设计模式举例
定义非线程安全的懒汉式单例实现
/**
* 懒汉式单例设计模式
*
* @Author YUBIN
* @create 2019-02-17
*/
public class LazySingleton {
private LazySingleton() {
}
private static volatile LazySingleton lazySingleton;
public static LazySingleton getInstance() {
if (lazySingleton == null) {
lazySingleton = new LazySingleton();
}
return lazySingleton;
}
}
定义一个线程类
/**
* 线程类
*
* @Author YUBIN
* @create 2019-02-17
*/
public class ThreadDemo extends Thread {
@Override
public void run() {
LazySingleton instance = LazySingleton.getInstance();
System.out.println(instance);
}
}
书写测试类
/**
* 单例设计模式测试类
*
* @Author YUBIN
* @create 2019-02-17
*/
public class Test {
public static void main(String[] args) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
thread1.start();
thread2.start();
System.out.println("program done");
}
}
在下列几个图处打上断点,并将线程挂起方式设置为Thread



以debug的形式启动Test类中的main方法


选择红框中的下拉框,即可切换到对应的线程的断点处。
首先选择Thread-0线程,此时会进入到下图位置

按F8跳转到

此时切换到Thread-1线程并执行完Thread-1线程,然后将剩余的线程执行完,即可看到一个不安全的懒汉式单例设计模式的演示
程序输出结果
com.yubin.design.pattern.creational.singleton.LazySingleton@212aa004
com.yubin.design.pattern.creational.singleton.LazySingleton@22fd7e02
program done