0

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);
}
}
user697911
  • 10,043
  • 25
  • 95
  • 169

2 Answers2

3

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.

Gray
  • 115,027
  • 24
  • 293
  • 354
2

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.

Vitaly
  • 2,760
  • 2
  • 19
  • 26
  • what's the importance of set a.join() as well? – user697911 Apr 13 '13 at 19:17
  • To continue debugging when all other threads go away. You have three threads, if you set BP in run() only then you catch two of them but the third (main app thread) will run away :) – Vitaly Apr 13 '13 at 19:21
  • Uh, main won't run away because it will be blocked on the `join` :-). – Gray Apr 13 '13 at 19:25
  • it's problematic. when i set both in run() and a.join(). It first directly goes to a.join(), not inside of run(). is this normal? shouldn't. – user697911 Apr 13 '13 at 19:41
  • that's Ok. it will wait on a.join() if you step it over. when thread A finishes it will raise in main thread again, you will step it over, main thread will wait again, when thread B finishes the main thread will be up again. Nothing complex :) BTW, the flow will slightly change if thread B finishes first - depends on what thread you "step over" first (you can choose the thread in Debug view). – Vitaly Apr 13 '13 at 19:47
  • Just switch to Debug perspective, it has Debug view with threads. http://www.vogella.com/articles/EclipseDebugging/images/debugstart20.gif This is the Debug perspective, the window in upper left is Debug view. – Vitaly Apr 13 '13 at 19:51
  • but it's struck over there. don't continue. – user697911 Apr 13 '13 at 19:56
  • yes, I do. I see, when the main thread is waiting, I should click one of the other threads to continue. However, how can i watch the value change of the static variable, 'count'? when I step into one of the threads, I can't see 'count' in variable view. – user697911 Apr 13 '13 at 20:19
  • http://stackoverflow.com/questions/801193/modify-view-static-variables-while-debugging-in-eclipse – Vitaly Apr 13 '13 at 20:24