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());
}
}
}
}
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 thestart()will work fine. It is the 2nd breakpoint that is holding up the other thread and stopping it from running thesleep()or returning to thejoin().In the following image you can see that I stepped over the
start()method andThread-1was started. But it is paused (see the yellow pause bars) waiting for you to switch to it and continue.