How to Debug a Multi-Threaded App in Intellij

How to debug a multi-threaded app in IntelliJ?

The documentation reads confusingly, but this is the relevant block. What it distills down to is setting the property to suspend on threads, and not the entire application instead. This will cause you to hit the break point on each individual thread instead of an arbitrary, indeterminate thread.

Suspend box checked with Thread radio button selected.

  • Suspend Policy: All

    • When a breakpoint is hit, all threads are suspended.
  • Suspend Policy: Thread

    • When the breakpoint is hit, the thread where the breakpoint is hit is suspended.

switching between threads in Intellij Idea

Trick is to set breakpoint suspend policy to - Thread.
View breakpoint properties (right-click on breakpoint)

breakpoint properties - suspend policy

Once done threads will hit breakpoint and block, now active thread can be switched to check race conditions/deadlocks.

switching between threads

Following code snippet for creating deadlock:

public static void main(String args[]) {
Thread thread1 = new Thread(null, new MyThread(obj1, obj2), "Thread-1");
Thread thread2 = new Thread(null, new MyThread(obj2, obj1), "Thread-2");
thread1.start();
thread2.start();
}
class MyThread implements Runnable {
private Object obj1;
private Object obj2;

MyThread(Object obj1, Object obj2) {
this.obj1 = obj1;
this.obj2 = obj2;
}

@Override
public void run() {
System.out.println("Acquiring locks");
synchronized (obj1){
System.out.println("Acquired 1st lock");
synchronized (obj2){
System.out.println("Acquired 2nd lock");
}
System.out.println("Released 2nd lock");
}
System.out.println("Released 1st lock");
}
}

Suspending Threads while Debugging Java Application IntelliJ

There's a setting exactly for this here:
Preferences | Build, Execution, Deployment | Debugger | Stepping - Resume only the current thread

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

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.

IntelliJ Thread Debug

I think you can. I have suspended threads via breakpoints by setting the suspend policy. This will suspend the thread that is executing this piece of code. If you have multiple thread then I would think they would carry on.

To quote the suspend policy

  • Item Description
  • All : When the breakpoint is hit, all threads are suspended
  • Thread : When the breakpoint is hit, the thread where the breakpoint is hit is suspended.
  • None: No thread is suspended.

Intellij Idea debug with thread

Try this: Run -> View breakpoints -> find your breakpoint -> First checkbox (Suspend). Try Setting suspend mode to Thread instead of All. So only thread where breakpoint occurs will be suspended instead of all threads in your application. Or try to disable Suspend for this breakpoint. Instead of suspending you can log some message to console. Look at Log message to console or Log evaluated expression options. This is for Idea 12. Idea 14 as I know has same options.



Related Topics



Leave a reply



Submit