ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/JSR166TestCase.java
(Generate patch)

Comparing jsr166/src/test/tck/JSR166TestCase.java (file contents):
Revision 1.148 by jsr166, Mon Sep 28 08:23:49 2015 UTC vs.
Revision 1.151 by jsr166, Sat Oct 3 19:19:01 2015 UTC

# Line 15 | Line 15 | import java.io.ObjectInputStream;
15   import java.io.ObjectOutputStream;
16   import java.lang.management.ManagementFactory;
17   import java.lang.management.ThreadInfo;
18 + import java.lang.management.ThreadMXBean;
19   import java.lang.reflect.Constructor;
20   import java.lang.reflect.Method;
21   import java.lang.reflect.Modifier;
# Line 743 | Line 744 | public class JSR166TestCase extends Test
744      }
745  
746      /**
747 +     * Allows use of try-with-resources with per-test thread pools.
748 +     */
749 +    static class PoolCloser<T extends ExecutorService>
750 +            implements AutoCloseable {
751 +        public final T pool;
752 +        public PoolCloser(T pool) { this.pool = pool; }
753 +        public void close() { joinPool(pool); }
754 +    }
755 +
756 +    /**
757       * Waits out termination of a thread pool or fails doing so.
758       */
759 <    void joinPool(ExecutorService pool) {
759 >    static void joinPool(ExecutorService pool) {
760          try {
761              pool.shutdown();
762              if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
# Line 767 | Line 778 | public class JSR166TestCase extends Test
778       * necessarily individually slow because they must block.
779       */
780      void testInParallel(Action ... actions) {
781 <        ExecutorService pool = Executors.newCachedThreadPool();
782 <        try {
781 >        try (PoolCloser<ExecutorService> poolCloser
782 >             = new PoolCloser<>(Executors.newCachedThreadPool())) {
783 >            ExecutorService pool = poolCloser.pool;
784              ArrayList<Future<?>> futures = new ArrayList<>(actions.length);
785              for (final Action action : actions)
786                  futures.add(pool.submit(new CheckedRunnable() {
# Line 781 | Line 793 | public class JSR166TestCase extends Test
793                  } catch (Exception ex) {
794                      threadUnexpectedException(ex);
795                  }
784        } finally {
785            joinPool(pool);
796          }
797      }
798  
799      /**
800       * A debugging tool to print all stack traces, as jstack does.
801 +     * Uninteresting threads are filtered out.
802       */
803      static void printAllStackTraces() {
804 <        for (ThreadInfo info :
805 <                 ManagementFactory.getThreadMXBean()
806 <                 .dumpAllThreads(true, true))
804 >        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
805 >        System.err.println("------ stacktrace dump start ------");
806 >        for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
807 >            String name = info.getThreadName();
808 >            if ("Signal Dispatcher".equals(name))
809 >                continue;
810 >            if ("Reference Handler".equals(name)
811 >                && info.getLockName().startsWith("java.lang.ref.Reference$Lock"))
812 >                continue;
813 >            if ("Finalizer".equals(name)
814 >                && info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock"))
815 >                continue;
816              System.err.print(info);
817 +        }
818 +        System.err.println("------ stacktrace dump end ------");
819      }
820  
821      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines