I am trying to run the following code with break points as follows:
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Starting"); //breakpoint here
}
}).start();
int i = 10;
i++; //breakpoint here
when this code runs ONLY the i++ breakpoint is hit... If I remove that one, the other thread's breakpoint would be hit correctly. Why is this weird behaviour occuring?
I just had this problem and for the sake of others that run into this, here is the reason for this behavior and how to change it.
As Doron pointed out, there is documentation concerning this. However, the IMPORTANT thing to notice is that by default, all threads in the JVM are suspended when a breakpoint is reached.
What you are expecting (and what I was expecting) is that only the thread with the breakpoint is suspended.
This isn't what you want, nor is it what I wanted.
To change this behavior (and provide the desired behavior).
1) Create a breakpoint by left clicking in margin.
2) Press ctrl+shift+F8 (to bring up the breakpoint menu).
3) Select your breakpoint. You will see options for it.
4) Make sure "Suspend" is checked and "Thread" radio option is selected.
5) Click the "Make default" button.
Now, when you run, you'll see that breakpoints in different threads are hit.
This is documented in http://www.jetbrains.com/idea/webhelp/breakpoints-2.html:
There are certain cases when IntelliJ IDEA will not stop at a breakpoint. Consider the following situation: Two breakpoints are set at the different methods of a class, and there suspend policy is set to All. When one of the breakpoints is hit, some step actions are performed. If at the time of stepping another thread hits the second breakpoint, IntelliJ IDEA will not stop there.
I copied your code example and recreated the situation.
Sure enough, like it says in the documentation, after stopping at the i++
breakpoint, if I hit F8 (step over) the program doesn't stop on the other breakpoint. But if I hit F9 (resume) the program does stop again on the other breakpoint.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With