Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intellij - not stopping on all breakpoints in multithreaded code

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?

like image 419
Bober02 Avatar asked Mar 08 '13 16:03

Bober02


2 Answers

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.

like image 114
Nathan Avatar answered Nov 09 '22 15:11

Nathan


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.

like image 28
Doron Gold Avatar answered Nov 09 '22 14:11

Doron Gold