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

Print this page




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  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 


  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(); }




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  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  * @author Martin Buchholz
  29  */
  30 
  31 import java.security.*;
  32 import java.util.*;
  33 import java.util.concurrent.*;
  34 import java.util.concurrent.atomic.*;
  35 import java.lang.reflect.Field;
  36 import sun.misc.Unsafe;
  37 
  38 public class ThrowingTasks {
  39     static final Unsafe UNSAFE;
  40     static {
  41         try {
  42             Field f = Unsafe.class.getDeclaredField("theUnsafe");
  43             f.setAccessible(true);
  44             UNSAFE = (Unsafe)f.get(null);
  45         } catch (NoSuchFieldException | IllegalAccessException e) {
  46             throw new RuntimeException(e);
  47         }
  48     }
  49 
  50     static final Random rnd = new Random();
  51 
  52     @SuppressWarnings("serial")
  53     static class UncaughtExceptions
  54         extends ConcurrentHashMap<Class<?>, Integer> {
  55 
  56         void inc(Class<?> key) {
  57             for (;;) {
  58                 Integer i = get(key);
  59                 if (i == null) {
  60                     if (putIfAbsent(key, 1) == null)
  61                         return;
  62                 } else {
  63                     if (replace(key, i, i + 1))
  64                         return;
  65                 }
  66             }
  67         }
  68     }
  69 


  97                 }};
  98 
  99     static final ThreadGroup tg = new ThreadGroup("Flaky");
 100 
 101     static final ThreadFactory tf = new ThreadFactory() {
 102             public Thread newThread(Runnable r) {
 103                 Thread t = new Thread(tg, r);
 104                 t.setUncaughtExceptionHandler(handler);
 105                 return t;
 106             }};
 107 
 108     static final RuntimeException rte = new RuntimeException();
 109     static final Error error = new Error();
 110     static final Throwable weird = new Throwable();
 111     static final Exception checkedException = new Exception();
 112 
 113     static class Thrower implements Runnable {
 114         Throwable t;
 115         Thrower(Throwable t) { this.t = t; }
 116         @SuppressWarnings("deprecation")
 117         public void run() { if (t != null) UNSAFE.throwException(t); }
 118     }
 119 
 120     static final Thrower noThrower      = new Thrower(null);
 121     static final Thrower rteThrower     = new Thrower(rte);
 122     static final Thrower errorThrower   = new Thrower(error);
 123     static final Thrower weirdThrower   = new Thrower(weird);
 124     static final Thrower checkedThrower = new Thrower(checkedException);
 125 
 126     static final List<Thrower> throwers = Arrays.asList(
 127         noThrower, rteThrower, errorThrower, weirdThrower, checkedThrower);
 128 
 129     static class Flaky implements Runnable {
 130         final Runnable beforeExecute;
 131         final Runnable execute;
 132         Flaky(Runnable beforeExecute,
 133               Runnable execute) {
 134             this.beforeExecute = beforeExecute;
 135             this.execute = execute;
 136         }
 137         public void run() { execute.run(); }