views:

32

answers:

1

Using the following textbook thread wait/notify example, is there a tool (Eclipse plugin?) that tracks which thread locks on which object while stepping through and debugging? A tool that visually displays the connections in some way would be ideal if possible.

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();
        }
    }
}
+2  A: 

Eclipse support this already. There is a symbol in the stack of the debug window where a synchronized is. If you enable "Show Monitors" then you can also see the object on which are locks. You can set it in the options of the debug view "Java | Show Monitors".

Horcrux7