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.
Start the debug session by clicking the Run button near the main method and selecting Debug. When the program has run, both threads are individually suspended in the addIfAbsent method. Now you can switch between the threads (in the Frames or Threads tab) and control the execution of each thread.
To continue the program execution after it has been suspended, press F9 or select Run | Debugging Actions | Resume from the main menu.
If you press and hold the Ctrl / ⌘ button while dragging the arrow, the IDE will highlight all the lines in green. When you drop the arrow, you won't jump to the line of code. Instead, the IDE will act as if you have used the Run to Cursor (⌥F9) action.
You can customize how threads are displayed on the Frames and Threads tabs. It might be helpful when you are working on a multithreaded application and often need to access information about your threads. Right-click anywhere in the Frames or Threads tab and select Customize Threads View.
By right click at the breakpoint dot, you can choose some options, e.g Suspend on Thread.
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