user697911
user697911

Reputation: 10531

Eclipse debugger dosn't work with this little two threads

How to debug this? I know this is not thread safe, but just want to keep track of the logic.

class Test {
public static int count = 0;
class CountThread extends Thread {

    public void run()
    {
        System.out.println(Thread.currentThread().getName() + " start");
        count++;
        System.out.println(Thread.currentThread().getName() + " end");
    }
}  

public void add(){
    CountThread a = new CountThread();
    CountThread b = new CountThread();

            a.start();
    b.start();

            try {
        a.join();
        b.join();
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }

}
public static void main(String[] args) {

    Test test = new Test();
    System.out.println("START = " + Test.count);

            test.add();

            System.out.println("END: Account balance = " + Test.count);
}

When it's executed to a.start(), in Eclipse I click "step into", it doesn't go to the run() method, but go to below, and there is no way to go to run() in my code. How to debug this?

     public synchronized void start() {
    /**
 * This method is not invoked for the main method thread or "system"
 * group threads created/set up by the VM. Any new functionality added 
 * to this method in the future may have to also be added to the VM.
 *
 * A zero status value corresponds to state "NEW".
     */
    if (threadStatus != 0 || this != me)
        throw new IllegalThreadStateException();
    group.add(this);
    start0();
    if (stopBeforeStart) {
    stop0(throwableFromStop);
}
}

Upvotes: 0

Views: 2019

Answers (2)

Gray
Gray

Reputation: 116878

When it's executed to a.start(), in Eclipse I click "step into", it doesn't go to the run() method, but go to [the Thread.start() method], and there is no way to go to run() in my code.

This is to be expected and is not some problem with Eclipse. You cannot debug into the native methods that are executing when a thread is forked and begins to execute.

How to debug this?

I would put a break point on the first line of your run() method. Or, if you must, you can put a break point in the Thread.run() method which is where your run() methods are called:

// the Thread.run() method
public void run() {
    if (target != null) {
        // Your `CountThread` objects are the `target`.
        target.run();
    }
}

NOTE: You need to understand that any debugging of this sort of program is going to vastly change the execution order of the threads. The System.out.println() calls do this as well because they produce IO and the underlying PrintStream is synchronized. This will mean that once you remove the print statements and the breakpoints your program is going to behave very different.

Upvotes: 3

Vitaly
Vitaly

Reputation: 2790

start() is not run().

You need to set breakpoint in run(). Then, run the app and eclipse will stay in run().

In Debug view you will see two threads.

Do not forget to set breakpoint at a.join(); too.

Upvotes: 2

Related Questions