void (final LinkedBlockingQueue<Pair<VirtualFile, GroovyDslExecutor>> queue, final Project project, final VirtualFile vfile, final long stamp, final String text) { final String fileUrl = vfile.getUrl(); final Runnable parseScript = () -> { GroovyDslExecutor executor = getCachedExecutor(vfile, stamp); try { if (executor == null && isActivated(vfile)) { executor = createExecutor(text, vfile, project); // executor is not only time-consuming to create, but also takes some PermGenSpace // => we can't afford garbage-collecting it together with PsiFile // => cache globally by file instance vfile.putUserData(CACHED_EXECUTOR, Pair.create(executor, stamp)); if (executor != null) { setStatusAndError(vfile, Status.ACTIVE, null); } } } finally { // access to our MultiMap should be synchronized synchronized (filesInProcessing) { // put evaluated executor to all queues for (LinkedBlockingQueue<Pair<VirtualFile, GroovyDslExecutor>> queue1 : filesInProcessing.remove(fileUrl)) { queue1.offer(Pair.create(vfile, executor)); } } } }; synchronized (filesInProcessing) { //ensure that only one thread calculates dsl executor final boolean isNewRequest = !filesInProcessing.containsKey(fileUrl); filesInProcessing.putValue(fileUrl, queue); if (isNewRequest) { ourPool.execute(parseScript); } } }