public class TestMain extends Thread {
@Override public void run() {
System.out.println(Thread.currentThread().getName() + " hello world");
}
public static void main(String[] args) {
new TestMain().start();
new TestMain().start();
}
}
關鍵點:斷點**必須設置在 run() 方法或其調用鏈內**,否則僅主線程進入 Debug,子線程直接跑完。

Thread-0、Thread-1... 

public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) { // 1 檢查
System.out.println(instance); // 2 打印
instance = new Singleton(); // 3 實例化
}
return instance;
}
}

兩線程打印同一地址,**偽裝成線程安全**

if (instance == null),**Suspend 線程 A**instance == null,**Suspend 線程 B**結果:
com.wxw.debug.Singleton@1fc0f04
com.wxw.debug.Singleton@13ded59
**兩個不同地址,證明非單例!**
不僅是解決 Bug 工具,更是**理解並發理論的顯微鏡**: