6
votes

While debugging multi-threaded code in intellij, and more specifically while stepping inside a thread...

Setup:

            @Override
            public
            void run() {
                while (true) {
                  System.err.println("1" + get());  <-- breakpoint
                  System.err.println("2" + get());
                  System.err.println("3" + get());
                }
            }

            public String get() {
               return "x";
            }

I have 6 threads, all running the code above.

Intellij keeps changing the current thread that I'm in, meaning that when I issue a "Step over" (F8) command for T1, the debugger will change focus to T2. What I'm expecting is to stay in T1 while I'm debugging -- so to produce the result:

T1: 1x
T1: 2x
T1: 3x
T1: 1x
T1: 2x

Then, let's switch to another thread, T2.

T2: 1x
T2: 2x
T2: 3x
T2: 1x
T2: 2x
T2: 3x

Then, back to T1:

T1: 3x  (it picks back up from where it left off)

Instead, what I'm getting is:

T1: 1x
T3: 1x
T5: 1x
T1: 2x
T2: 1x
T3: 2x
T1: 3x
etc.

It's out-of-order, and not at all intuitive.

If I want to have my intended behavior, I have to do the following:

Switch to T1:

T1: 1x

Switch to T1:

T1: 2x

Switch to T1:

T1: 3x

Switch to T2:

T2: 1x

Switch to T2:

T2: 2x

Switch to T2:

T2: 3x

Switch to T1:

T1: 1x

etc...

I've searched for a way to prevent this, and I've not found a solution, and the only way that I've discovered (to maintain stepping in my current thread), is to keep selecting it with the mouse every time I step.

The question is: how do I do prevent intellij changing the thread I'm on?

edit: I should point out, that if any methods are called, thread execution order is out-of-order.

3
While related, I'm not interested in stopping the entire VM, and debugging at that point. My issue is that IDEA is skipping between threads during the debug process -- I just want it to stay in the current thread while stepping. - Nathan
Looks like this might have been fixed 3 weeks ago in EAP 15. - Nathan

3 Answers

2
votes

By right click at the breakpoint dot, you can choose some options, e.g Suspend on Thread.

enter image description here

2
votes

Indeed, IDEA 15 (http://blog.jetbrains.com/idea/2015/06/intellij-idea-15-eap-is-open/) has fixed this issue.

Additionally, when a breakpoint is reached in another thread, there is a notification - which I found rather nice.

0
votes

From Intellij IDEA 16 onwards there'a new twist around this issue by resuming only the curent thread, and it can be tested in the latest IDEA 16 EAP by enabling "debugger.step.resumes.one.thread" property in IDEA vmoptions