Walker
Walker

Reputation: 1225

Hang on stepping over thread run() with breakpoints

I'm running into a problem new to me when trying to debug a multithreaded program. Below, I've included a test program demonstrating my issue. Essentially, when I put a breakpoint on my call to a thread's start() method, as well as on that thread's run() method, things are fine if I "continue" in the debugger. If I step over, though, I can step over the calls to start(), println(), and join(), after which the program waits for execution to finish, but that never happens. The thread doesn't seems to execute, and my breakpoint in the thread's run() never trips. I have to quit the program.

Now if there's no breakpoint on run(), it doesn't matter whether I continue or step; the thread executes. What am I missing here?

Note that I am using vanilla eclipse debugging.

public class Test {
    public static void main(String[] args) {
        try {
            Thread myThread = new Test().new TestThread();
            long threadStartTime = System.currentTimeMillis();
            myThread.start(); // **********Breakpoint 1
            System.out.println("Thread started");
            myThread.join();
            long threadExecutionTime = System.currentTimeMillis() - threadStartTime;
            System.out.println("Thread finished execution in " + threadExecutionTime + " milliseconds.");
        } catch(InterruptedException e) {
            System.err.println(e.getMessage());
        }
    }

    private class TestThread extends Thread {
        @Override
        public void run() { // **********Breakpoint 2
            try {
                sleep(5*1000);
            } catch(InterruptedException e) {
                System.err.println(e.getMessage());
            }
        }
    }
}

Upvotes: 1

Views: 9475

Answers (2)

Gray
Gray

Reputation: 116878

Are you sure that the breakpoint in run() isn't being tripped? In Eclipse, you need to click on the 2nd thread to see that it is paused. I suspect that the thread has been started but is waiting for you to say continue.

I suspect that if you don't have a breakpoint in run() then stepping over the start() will work fine. It is the 2nd breakpoint that is holding up the other thread and stopping it from running the sleep() or returning to the join().

In the following image you can see that I stepped over the start() method and Thread-1 was started. But it is paused (see the yellow pause bars) waiting for you to switch to it and continue.

enter image description here

Upvotes: 3

tibtof
tibtof

Reputation: 7957

If you are debugging using eclipse, you should see the list of threads in debug window. Select the other thread's stacktrace from there and you should be able to debug run method, too.

Upvotes: 3

Related Questions