test/java/util/concurrent/locks/Lock/FlakyMutex.java

Print this page




  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 6503247 6574123
  27  * @summary Test resilience to tryAcquire methods that throw
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.util.*;
  32 import java.util.concurrent.*;
  33 import java.util.concurrent.locks.*;


  34 
  35 /**
  36  * This uses a variant of the standard Mutex demo, except with a
  37  * tryAcquire method that randomly throws various Throwable
  38  * subclasses.
  39  */
  40 @SuppressWarnings({"deprecation", "serial"})
  41 public class FlakyMutex implements Lock {











  42     static class MyError extends Error {}
  43     static class MyException extends Exception {}
  44     static class MyRuntimeException extends RuntimeException {}
  45 
  46     static final Random rnd = new Random();
  47 
  48     static void maybeThrow() {
  49         switch (rnd.nextInt(10)) {
  50         case 0: throw new MyError();
  51         case 1: throw new MyRuntimeException();
  52         case 2: Thread.currentThread().stop(new MyException()); break;
  53         default: /* Do nothing */ break;
  54         }
  55     }
  56 
  57     static void checkThrowable(Throwable t) {
  58         check((t instanceof MyError) ||
  59               (t instanceof MyException) ||
  60               (t instanceof MyRuntimeException));
  61     }
  62 
  63     static void realMain(String[] args) throws Throwable {
  64         final int nThreads = 3;
  65         final CyclicBarrier barrier = new CyclicBarrier(nThreads + 1);
  66         final FlakyMutex m = new FlakyMutex();
  67         final ExecutorService es = Executors.newFixedThreadPool(nThreads);
  68         for (int i = 0; i < nThreads; i++) {
  69             es.submit(new Runnable() { public void run() {
  70                 try {
  71                     barrier.await();
  72                     for (int i = 0; i < 10000; i++) {




  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 6503247 6574123
  27  * @summary Test resilience to tryAcquire methods that throw
  28  * @author Martin Buchholz
  29  */
  30 
  31 import java.util.*;
  32 import java.util.concurrent.*;
  33 import java.util.concurrent.locks.*;
  34 import java.lang.reflect.Field;
  35 import sun.misc.Unsafe;
  36 
  37 /**
  38  * This uses a variant of the standard Mutex demo, except with a
  39  * tryAcquire method that randomly throws various Throwable
  40  * subclasses.
  41  */
  42 @SuppressWarnings({"deprecation", "serial"})
  43 public class FlakyMutex implements Lock {
  44     private static final Unsafe UNSAFE;
  45     static {
  46         try {
  47             Field f = Unsafe.class.getDeclaredField("theUnsafe");
  48             f.setAccessible(true);
  49             UNSAFE = (Unsafe)f.get(null);
  50         } catch (NoSuchFieldException | IllegalAccessException e) {
  51             throw new RuntimeException(e);
  52         }
  53     }
  54 
  55     static class MyError extends Error {}
  56     static class MyException extends Exception {}
  57     static class MyRuntimeException extends RuntimeException {}
  58 
  59     static final Random rnd = new Random();
  60 
  61     static void maybeThrow() {
  62         switch (rnd.nextInt(10)) {
  63         case 0: throw new MyError();
  64         case 1: throw new MyRuntimeException();
  65         case 2: UNSAFE.throwException(new MyException()); break;
  66         default: /* Do nothing */ break;
  67         }
  68     }
  69 
  70     static void checkThrowable(Throwable t) {
  71         check((t instanceof MyError) ||
  72               (t instanceof MyException) ||
  73               (t instanceof MyRuntimeException));
  74     }
  75 
  76     static void realMain(String[] args) throws Throwable {
  77         final int nThreads = 3;
  78         final CyclicBarrier barrier = new CyclicBarrier(nThreads + 1);
  79         final FlakyMutex m = new FlakyMutex();
  80         final ExecutorService es = Executors.newFixedThreadPool(nThreads);
  81         for (int i = 0; i < nThreads; i++) {
  82             es.submit(new Runnable() { public void run() {
  83                 try {
  84                     barrier.await();
  85                     for (int i = 0; i < 10000; i++) {