Error writing data in a tree provided by a background thread
I'm trying to write a data read from a long running background thread in my component tree. I've tried to use runReadAction and runWriteAction methods to do it in the correct threads but it's not working. The runWriteAction is not running because even being executed in the writing thread the writing lock is false.
The method bellow is called internally by the runWriteAction method to check the correct writing state. Checking it debugging the code the isWriteThread() method is returning true but the myLock.isWriteLocked() method is returning false.
public boolean isWriteAccessAllowed() {
return isWriteThread() && myLock.isWriteLocked();
}
This is my code:
ApplicationManager.getApplication().executeOnPooledThread(() -> {
ApplicationManager.getApplication().runReadAction(() -> {
System.out.println("LOADING OBJECTS");
connectionLoadDispatcher.getMulticaster().onConnectionLoad(connection);
System.out.println("COMPLETED 1");
});
ApplicationManager.getApplication().invokeLaterOnWriteThread(() -> {
ApplicationManager.getApplication().runWriteAction(() -> {
System.out.println("RENDERING OBJECTS");
loadSObjects(connection, node);
System.out.println("COMPLETED 2");
});
}, ModalityState.NON_MODAL);
});
How can I do to get the write lock?
Please sign in to leave a comment.
Could you please show bigger context of what you're building here? com.intellij.openapi.application.Application#invokeLaterOnWriteThread(java.lang.Runnable) is Experimental API and must not be used by plugins. See also https://jetbrains.org/intellij/sdk/docs/basics/architectural_overview/general_threading_rules.html for summary.
Ensure that UI-related tasks are executed on the Event Dispatch Thread. The invokeLaterOnWriteThread method should be used for UI-related actions, and invokeLater for actions that don't require a write lock.
ApplicationManager.getApplication().invokeLater(() -> {
// UI-related actions here
});
Ensure that
runReadAction
is used for read access andrunWriteAction
is used for write access. Also, be aware that long-running tasks should not be performed inside the EDT. Instead, use pooled threads for time-consuming operations.ApplicationManager.getApplication().executeOnPooledThread(() -> {
ApplicationManager.getApplication().runReadAction(() -> {
// Read access code here
});
ApplicationManager.getApplication().invokeLater(() -> {
ApplicationManager.getApplication().runWriteAction(() -> {
// Write access code here
});
}, ModalityState.NON_MODAL);
});
Mixing EDT and pooled threads can lead to synchronization issues. Make sure that your code consistently follows the threading model recommended by IntelliJ.
ApplicationManager.getApplication().executeOnPooledThread(() -> {
// Perform background tasks here
ApplicationManager.getApplication().invokeLater(() -> {
// UI-related tasks here
});
});
IntelliJ IDEA provides b2b data enrichment tools to analyze threading issues. You can use the "Analyze Stacktrace" feature to identify potential problems related to threading.
View -> Tool Windows -> Threads
).