代码之家  ›  专栏  ›  技术社区  ›  Christopher Tokar

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

  •  3
  • Christopher Tokar  · 技术社区  · 15 年前

    使用以下教科书中的线程等待/通知示例,是否有工具(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();
            }
        }
    }
    
    1 回复  |  直到 15 年前
        1
  •  5
  •   Horcrux7    15 年前

    Eclipse已经支持这个了。在调试窗口的堆栈中有一个符号,其中同步了。如果启用“Show Monitors”,那么还可以看到锁所在的对象。您可以在调试视图“Java | Show Monitors”的选项中进行设置。