Better way to handling threading?



I have read the General Threading Rules documentation and also know there is a plugin named "Thread Access Info" for helping us use the correct thread when developing the plugins.

But I still found that using `ApplicationManager.getApplication().runWriteAction()` and `ApplicationManager.getApplication().invokeLater()`, etc are a little verbose and error-prone in some cases which

1. easily cause the concurrency issue like dead lock, do something heavier thing which freezes the UI thread.

2. we have a team working on developing the IDEA plugins and we found non-experienced developers would misuse them easily and they need have a deep understanding of threading rules to avoid them.


public class XXXAction extends DumbAwareAction {

  public void actionPerformed(@NotNull AnActionEvent e) {


    // the above code is incorrect and the correct way is to put this a task into non-UI thread



The questions:

1. Do you have any plan to have better APIs or better ways to make it easier to write code which easier to handle thread and is less easy to cause the thread lock or prevent writing code that freezes the IDE easily? E.g. provides an API using the Kotlin coroutine or something make it easier to switch the thread and less error-prone.

2. When writing code (without using the "Thread Access Info" for debugging), is there any way to easy way to know if the current method (like the example) is invoked in the UI thread or maybe I need to assume most APIs I used or the API method I override is invoked in the UI thread?

3. Any other resources/tools/documentation or ways that could help us write code or learn how to hand these with less possibility to cause the UI freeze, deadlock, and similar issues?

Thanks a lot.

1 comment
Comment actions Permalink

1. There is some support for using coroutines, see com/intellij/openapi/application/coroutines.kt & com/intellij/openapi/progress/coroutines.kt. Note that some parts are still experimental. Note: do _not_ bundle any coroutine libraries in your plugin, but use those provided by the platform  and make sure to build against the lowest supported target IDE version.

2. Some methods are annotated with com.intellij.util.concurrency.annotations.RequiresEdt. Various methods exist in Application to assert thread/R/W-access, e.g. com.intellij.openapi.application.Application#assertIsDispatchThread

3. Above mentioned page has a number of pointers and guides on how to avoid freezes. If you have specific issues or questions, it's best to discuss actual code.


Please sign in to leave a comment.