Search code examples
javamultithreadingdebuggingintellij-ideaintellij-14

How to stay on the current thread while step-debugging in intellij


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.


Solution

  • 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.