thread leaks
authorAlexey Kudravtsev <cdr@intellij.com>
Fri, 6 Nov 2015 15:05:09 +0000 (18:05 +0300)
committerAlexey Kudravtsev <cdr@intellij.com>
Mon, 9 Nov 2015 11:21:16 +0000 (14:21 +0300)
xml/dom-tests/tests/com/intellij/util/xml/DomConcurrencyStressTest.java

index c625a6a2070a43612ebb86053e33101a21fcd532..82f2f804c5c8f5e30471d2056d65f91afa364ab7 100644 (file)
@@ -27,7 +27,10 @@ import com.intellij.psi.xml.XmlTag;
 import com.intellij.semantic.SemService;
 import com.intellij.testFramework.PlatformTestUtil;
 import com.intellij.testFramework.Timings;
+import com.intellij.util.Function;
+import com.intellij.util.Processor;
 import com.intellij.util.TimeoutUtil;
+import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.xml.impl.DomFileElementImpl;
 import com.intellij.util.xml.impl.DomTestCase;
 import com.intellij.util.xml.reflect.DomExtender;
@@ -35,6 +38,7 @@ import com.intellij.util.xml.reflect.DomExtenderEP;
 import com.intellij.util.xml.reflect.DomExtensionsRegistrar;
 import org.jetbrains.annotations.NotNull;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.Random;
 import java.util.concurrent.CountDownLatch;
@@ -115,27 +119,42 @@ public class DomConcurrencyStressTest extends DomTestCase {
     for (int i=0; i<threadCount/8 + 1; i++) {
       final Ref<Throwable> exc = Ref.create(null);
 
-      final CountDownLatch reads = new CountDownLatch(8);
-      for (int j = 0; j < 8; j++) {
-        new Thread("dom concurrency"){
-          @Override
-          public void run() {
-            try {
-              runnable.run();
-            }
-            catch (Throwable e) {
-              exc.set(e);
-            }
-            finally {
-              reads.countDown();
+      int N = 8;
+      final CountDownLatch reads = new CountDownLatch(N);
+      List<Thread> threads = ContainerUtil.map(Collections.nCopies(N, ""), new Function<String, Thread>() {
+        @Override
+        public Thread fun(String s) {
+          return new Thread("dom concurrency") {
+            @Override
+            public void run() {
+              try {
+                runnable.run();
+              }
+              catch (Throwable e) {
+                exc.set(e);
+              }
+              finally {
+                reads.countDown();
+              }
             }
-          }
-        }.start();
-      }
+          };
+        }
+      });
+      ContainerUtil.process(threads, new Processor<Thread>() {
+        @Override
+        public boolean process(Thread thread) {
+          thread.start();
+          return true;
+        }
+      });
+
       reads.await();
       if (!exc.isNull()) {
         throw exc.get();
       }
+      for (Thread thread : threads) {
+        thread.join();
+      }
     }
   }