test/java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java

Print this page




  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6450200 6450205 6450207 6450211
  27  * @summary Test proper handling of tasks that terminate abruptly
  28  * @run main/othervm -XX:-UseVMInterruptibleIO ThrowingTasks
  29  * @author Martin Buchholz
  30  */
  31 
  32 import java.security.*;
  33 import java.util.*;
  34 import java.util.concurrent.*;
  35 import java.util.concurrent.atomic.*;
  36 
  37 public class ThrowingTasks {
  38     final static Random rnd = new Random();
  39 
  40     @SuppressWarnings("serial")
  41     static class UncaughtExceptions
  42         extends ConcurrentHashMap<Class<?>, Integer> {
  43 
  44         void inc(Class<?> key) {
  45             for (;;) {
  46                 Integer i = get(key);
  47                 if (i == null) {
  48                     if (putIfAbsent(key, 1) == null)
  49                         return;
  50                 } else {
  51                     if (replace(key, i, i + 1))
  52                         return;
  53                 }
  54             }
  55         }
  56     }
  57 
  58     @SuppressWarnings("serial")
  59     static class UncaughtExceptionsTable
  60         extends Hashtable<Class<?>, Integer> {
  61 
  62         synchronized void inc(Class<?> key) {
  63             Integer i = get(key);
  64             put(key, (i == null) ? 1 : i + 1);
  65         }
  66     }
  67 
  68     final static UncaughtExceptions uncaughtExceptions
  69         = new UncaughtExceptions();
  70     final static UncaughtExceptionsTable uncaughtExceptionsTable
  71         = new UncaughtExceptionsTable();
  72     final static AtomicLong totalUncaughtExceptions
  73         = new AtomicLong(0);
  74     final static CountDownLatch uncaughtExceptionsLatch
  75         = new CountDownLatch(24);
  76 
  77     final static Thread.UncaughtExceptionHandler handler
  78         = new Thread.UncaughtExceptionHandler() {
  79                 public void uncaughtException(Thread t, Throwable e) {
  80                     check(! Thread.currentThread().isInterrupted());
  81                     totalUncaughtExceptions.getAndIncrement();
  82                     uncaughtExceptions.inc(e.getClass());
  83                     uncaughtExceptionsTable.inc(e.getClass());
  84                     uncaughtExceptionsLatch.countDown();
  85                 }};
  86 
  87     final static ThreadGroup tg = new ThreadGroup("Flaky");
  88 
  89     final static ThreadFactory tf = new ThreadFactory() {
  90             public Thread newThread(Runnable r) {
  91                 Thread t = new Thread(tg, r);
  92                 t.setUncaughtExceptionHandler(handler);
  93                 return t;
  94             }};
  95 
  96     final static RuntimeException rte = new RuntimeException();
  97     final static Error error = new Error();
  98     final static Throwable weird = new Throwable();
  99     final static Exception checkedException = new Exception();
 100 
 101     static class Thrower implements Runnable {
 102         Throwable t;
 103         Thrower(Throwable t) { this.t = t; }
 104         @SuppressWarnings("deprecation")
 105         public void run() { if (t != null) Thread.currentThread().stop(t); }
 106     }
 107 
 108     final static Thrower noThrower      = new Thrower(null);
 109     final static Thrower rteThrower     = new Thrower(rte);
 110     final static Thrower errorThrower   = new Thrower(error);
 111     final static Thrower weirdThrower   = new Thrower(weird);
 112     final static Thrower checkedThrower = new Thrower(checkedException);
 113 
 114     final static List<Thrower> throwers = Arrays.asList(
 115         noThrower, rteThrower, errorThrower, weirdThrower, checkedThrower);
 116 
 117     static class Flaky implements Runnable {
 118         final Runnable beforeExecute;
 119         final Runnable execute;
 120         Flaky(Runnable beforeExecute,
 121               Runnable execute) {
 122             this.beforeExecute = beforeExecute;
 123             this.execute = execute;
 124         }
 125         public void run() { execute.run(); }
 126     }
 127 
 128     static final List<Flaky> flakes = new ArrayList<Flaky>();
 129     static {
 130         for (Thrower x : throwers)
 131             for (Thrower y : throwers)
 132                 flakes.add(new Flaky(x, y));
 133         Collections.shuffle(flakes);
 134     }




  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6450200 6450205 6450207 6450211
  27  * @summary Test proper handling of tasks that terminate abruptly
  28  * @run main/othervm -XX:-UseVMInterruptibleIO ThrowingTasks
  29  * @author Martin Buchholz
  30  */
  31 
  32 import java.security.*;
  33 import java.util.*;
  34 import java.util.concurrent.*;
  35 import java.util.concurrent.atomic.*;
  36 
  37 public class ThrowingTasks {
  38     static final Random rnd = new Random();
  39 
  40     @SuppressWarnings("serial")
  41     static class UncaughtExceptions
  42         extends ConcurrentHashMap<Class<?>, Integer> {
  43 
  44         void inc(Class<?> key) {
  45             for (;;) {
  46                 Integer i = get(key);
  47                 if (i == null) {
  48                     if (putIfAbsent(key, 1) == null)
  49                         return;
  50                 } else {
  51                     if (replace(key, i, i + 1))
  52                         return;
  53                 }
  54             }
  55         }
  56     }
  57 
  58     @SuppressWarnings("serial")
  59     static class UncaughtExceptionsTable
  60         extends Hashtable<Class<?>, Integer> {
  61 
  62         synchronized void inc(Class<?> key) {
  63             Integer i = get(key);
  64             put(key, (i == null) ? 1 : i + 1);
  65         }
  66     }
  67 
  68     static final UncaughtExceptions uncaughtExceptions
  69         = new UncaughtExceptions();
  70     static final UncaughtExceptionsTable uncaughtExceptionsTable
  71         = new UncaughtExceptionsTable();
  72     static final AtomicLong totalUncaughtExceptions
  73         = new AtomicLong(0);
  74     static final CountDownLatch uncaughtExceptionsLatch
  75         = new CountDownLatch(24);
  76 
  77     static final Thread.UncaughtExceptionHandler handler
  78         = new Thread.UncaughtExceptionHandler() {
  79                 public void uncaughtException(Thread t, Throwable e) {
  80                     check(! Thread.currentThread().isInterrupted());
  81                     totalUncaughtExceptions.getAndIncrement();
  82                     uncaughtExceptions.inc(e.getClass());
  83                     uncaughtExceptionsTable.inc(e.getClass());
  84                     uncaughtExceptionsLatch.countDown();
  85                 }};
  86 
  87     static final ThreadGroup tg = new ThreadGroup("Flaky");
  88 
  89     static final ThreadFactory tf = new ThreadFactory() {
  90             public Thread newThread(Runnable r) {
  91                 Thread t = new Thread(tg, r);
  92                 t.setUncaughtExceptionHandler(handler);
  93                 return t;
  94             }};
  95 
  96     static final RuntimeException rte = new RuntimeException();
  97     static final Error error = new Error();
  98     static final Throwable weird = new Throwable();
  99     static final Exception checkedException = new Exception();
 100 
 101     static class Thrower implements Runnable {
 102         Throwable t;
 103         Thrower(Throwable t) { this.t = t; }
 104         @SuppressWarnings("deprecation")
 105         public void run() { if (t != null) Thread.currentThread().stop(t); }
 106     }
 107 
 108     static final Thrower noThrower      = new Thrower(null);
 109     static final Thrower rteThrower     = new Thrower(rte);
 110     static final Thrower errorThrower   = new Thrower(error);
 111     static final Thrower weirdThrower   = new Thrower(weird);
 112     static final Thrower checkedThrower = new Thrower(checkedException);
 113 
 114     static final List<Thrower> throwers = Arrays.asList(
 115         noThrower, rteThrower, errorThrower, weirdThrower, checkedThrower);
 116 
 117     static class Flaky implements Runnable {
 118         final Runnable beforeExecute;
 119         final Runnable execute;
 120         Flaky(Runnable beforeExecute,
 121               Runnable execute) {
 122             this.beforeExecute = beforeExecute;
 123             this.execute = execute;
 124         }
 125         public void run() { execute.run(); }
 126     }
 127 
 128     static final List<Flaky> flakes = new ArrayList<Flaky>();
 129     static {
 130         for (Thrower x : throwers)
 131             for (Thrower y : throwers)
 132                 flakes.add(new Flaky(x, y));
 133         Collections.shuffle(flakes);
 134     }