diff --git a/topics/basics/architectural_overview/threading/threading_model.md b/topics/basics/architectural_overview/threading/threading_model.md index 0b69e118a..7be6467db 100644 --- a/topics/basics/architectural_overview/threading/threading_model.md +++ b/topics/basics/architectural_overview/threading/threading_model.md @@ -268,6 +268,10 @@ In all other cases, it is required to wrap read operation in a read action with The read objects aren't guaranteed to survive between several consecutive read actions. Whenever starting a read action, check if the PSI/VFS/project/module is still valid. Example: + + + + ```kotlin val virtualFile = runReadAction { // read action 1 // read a virtual file @@ -280,6 +284,25 @@ val psiFile = runReadAction { // read action 2 } ``` + + + +```java +VirtualFile virtualFile = ReadAction.compute(() -> { + // read a virtual file +}); +// do other time-consuming work... +PsiFile psiFile = ReadAction.compute(() -> { // read action 2 + if (virtualFile.isValid()) { // check if the virtual file is valid + return PsiManager.getInstance(project).findFile(virtualFile); + } + return null; +}); +``` + + + + Between executing first and second read actions, another thread could invalidate the virtual file: ```mermaid