3

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
  • ( Note, wait should be in a while loop. You might also want to try java.util.conncurrent (I mean the collections and stuff, not java.util.concurrent.locks). ) Commented Sep 24, 2010 at 20:28
  • Also, as mentioned in an answer to another question using this code example, wait/notify should not be used on Thread objects, as those methods are also used by Thread itself. Commented Jul 31, 2013 at 9:18

1 Answer 1

5

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".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.