From 98b59e5a85bc18e99c758f20f2c586e589929155 Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Mon, 24 Feb 2025 12:30:34 +0100 Subject: [PATCH] threading_model.md: Add Java code example to Objects Validity --- .../threading/threading_model.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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