Debug asynchronous code

Last modified: 17 May 2021

Enable/disable: Settings/Preferences | Build, execution, deployment | Debugger | Async stack traces | Instrumenting agent

Debugging asynchronous code is a challenge because the tasks are often scheduled in one thread and executed in another. Every thread has its own stacktrace, making it difficult to figure out what happened before the thread started.

IntelliJ IDEA makes it easier by establishing a connection between frames in different threads. This allows you to look back from a worker thread to the place where the task was scheduled and debug the program as if the execution was all in the same thread.

To try async stack traces, debug the following example:

When we stop at the breakpoint in the printNum() method, there are two stacktraces available to us:

  • The current thread (worker)

  • The main thread (where the task was scheduled)

Async stacktraces