less GroovyDslFileIndex threads
authorpeter <peter@jetbrains.com>
Fri, 6 Feb 2015 11:21:42 +0000 (12:21 +0100)
committerpeter <peter@jetbrains.com>
Fri, 6 Feb 2015 11:23:41 +0000 (12:23 +0100)
platform/core-impl/src/com/intellij/openapi/application/ex/ApplicationUtil.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/dsl/GroovyDslFileIndex.java

index 451d3e587ed02f9fbcaa40168c4e56eef61dd489..fac3451878b3ede567e3bebabe88d5d9153d1aee 100644 (file)
@@ -23,11 +23,9 @@ import com.intellij.openapi.util.Computable;
 import com.intellij.openapi.util.Ref;
 import com.intellij.util.ExceptionUtil;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.ide.PooledThreadExecutor;
 
-import java.util.concurrent.Callable;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
+import java.util.concurrent.*;
 
 public class ApplicationUtil {
   // throws exception if can't grab read action right now
@@ -54,11 +52,21 @@ public class ApplicationUtil {
    * Allows to interrupt a process which does not performs checkCancelled() calls by itself.
    * Note that the process may continue to run in background indefinitely - so <b>avoid using this method unless absolutely needed</b>.
    */
-  public static <T> T runWithCheckCanceled(@NotNull final Callable<T> callable, @NotNull final ProgressIndicator indicator) throws Exception {
+  public static <T> T runWithCheckCanceled(@NotNull final Callable<T> callable,
+                                           @NotNull final ProgressIndicator indicator) throws Exception {
+    return runWithCheckCanceled(callable, indicator, PooledThreadExecutor.INSTANCE);
+  }
+
+  /**
+   * Allows to interrupt a process which does not performs checkCancelled() calls by itself.
+   * Note that the process may continue to run in background indefinitely - so <b>avoid using this method unless absolutely needed</b>.
+   */
+  public static <T> T runWithCheckCanceled(@NotNull final Callable<T> callable,
+                                           @NotNull final ProgressIndicator indicator, ExecutorService executorService) throws Exception {
     final Ref<T> result = Ref.create();
     final Ref<Throwable> error = Ref.create();
 
-    Future<?> future = ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
+    Future<?> future = executorService.submit(new Runnable() {
       @Override
       public void run() {
         ProgressManager.getInstance().executeProcessUnderProgress(new Runnable() {
index 692aab0efc4c374b6d9e995963e8311a8905dd61..0499332ee732bd15410243138d0f5a100aaebe19 100644 (file)
@@ -26,7 +26,10 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.project.ProjectManager;
 import com.intellij.openapi.roots.ProjectFileIndex;
 import com.intellij.openapi.roots.ProjectRootManager;
-import com.intellij.openapi.util.*;
+import com.intellij.openapi.util.Key;
+import com.intellij.openapi.util.ModificationTracker;
+import com.intellij.openapi.util.Pair;
+import com.intellij.openapi.util.Trinity;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
@@ -43,6 +46,7 @@ import com.intellij.psi.util.CachedValueProvider;
 import com.intellij.psi.util.CachedValuesManager;
 import com.intellij.psi.util.PsiModificationTracker;
 import com.intellij.reference.SoftReference;
+import com.intellij.util.ConcurrencyUtil;
 import com.intellij.util.ExceptionUtil;
 import com.intellij.util.Function;
 import com.intellij.util.PathUtil;
@@ -66,7 +70,10 @@ import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil;
 import java.io.File;
 import java.io.IOException;
 import java.util.*;
-import java.util.concurrent.*;
+import java.util.concurrent.Callable;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 import java.util.regex.Pattern;
 
 /**
@@ -84,6 +91,8 @@ public class GroovyDslFileIndex extends ScalarIndexExtension<String> {
   private static final MultiMap<String, LinkedBlockingQueue<Pair<VirtualFile, GroovyDslExecutor>>> filesInProcessing =
     new ConcurrentMultiMap<String, LinkedBlockingQueue<Pair<VirtualFile, GroovyDslExecutor>>>();
 
+  private static final ThreadPoolExecutor ourPool = new ThreadPoolExecutor(0, 4, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), ConcurrencyUtil.newNamedThreadFactory("Groovy DSL File Index Executor"));
+
   private final EnumeratorStringDescriptor myKeyDescriptor = new EnumeratorStringDescriptor();
 
   public GroovyDslFileIndex() {
@@ -366,7 +375,7 @@ public class GroovyDslFileIndex extends ScalarIndexExtension<String> {
       if (ApplicationManager.getApplication().isDispatchThread()) {
         return action.call();
       }
-      return ApplicationUtil.runWithCheckCanceled(action, new EmptyProgressIndicator());
+      return ApplicationUtil.runWithCheckCanceled(action, new EmptyProgressIndicator(), ourPool);
     }
     catch (Exception e) {
       ExceptionUtil.rethrowUnchecked(e);
@@ -414,7 +423,7 @@ public class GroovyDslFileIndex extends ScalarIndexExtension<String> {
           }
           if (fileIndex.isInLibrarySource(vfile)) {
             continue;
-          } 
+          }
           if (!fileIndex.isInLibraryClasses(vfile)) {
             if (!fileIndex.isInSourceContent(vfile) || !isActivated(vfile)) {
               continue;
@@ -513,7 +522,7 @@ public class GroovyDslFileIndex extends ScalarIndexExtension<String> {
       final boolean isNewRequest = !filesInProcessing.containsKey(fileUrl);
       filesInProcessing.putValue(fileUrl, queue);
       if (isNewRequest) {
-        ApplicationManager.getApplication().executeOnPooledThread(parseScript);
+        ourPool.execute(parseScript);
       }
     }
   }