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 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)
Once done threads will hit breakpoint and block, now active thread can be switched to check race conditions/deadlocks.
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
Java Naming Convention with Acronyms
Write a Program That Will Surely Go into Deadlock
Steps Needed to Use MySQL Database with Play Framework 2.0
How to Improve the Performance of G.Drawimage() Method for Resizing Images
How Much Memory Does a String Use in Java 8
What Is an "Internal Address" in Java
How to Make Hashmap Work with Arrays as Key
How to Use Selenium Webdriver on Local Webpage (On My Pc) Instead of One Located Somewhere Online
Java.Lang.Classnotfoundexception: Org.Springframework.Boot.Springapplication Maven
Write a File in Utf-8 Using Filewriter (Java)
In Java 8, Why Is the Default Capacity of Arraylist Now Zero
How to Convert Set<String> to String[]
How to Convert a Hexadecimal String to Long in Java
Parsing JSON Array Within JSON Object