在调试时,如何直观地表示哪些线程锁定了Java中的哪些对象?

Chr*_*kar 3 java eclipse multithreading

使用以下教科书线程等待/通知示例,是否有一个工具(Eclipse插件?),可以跟踪哪些线程在单步执行和调试时锁定哪个对象?如果可能的话,以某种方式可视地显示连接的工具将是理想的.

public class ThreadA {
    public static void main(String[] args) {
        ThreadB b = new ThreadB();
        b.start();
        synchronized (b) {      
            try {
                System.out.println("Waiting for b to complete...");
                b.wait();
            } catch (InterruptedException e) {
            }
            System.out.println("Total is: " + b.total);
        }
    }
}

class ThreadB extends Thread {
    int total;
    public void run() {
        synchronized (this) {
            for (int i = 0; i < 100; i++) {
                System.out.println(i);
                total += i;
            }
            notify();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Hor*_*ux7 5

Eclipse已经支持了这一点.调试窗口的堆栈中有一个符号同步的符号.如果启用"显示监视器",则还可以看到锁定的对象.您可以在调试视图"Java | Show Monitors"的选项中进行设置.