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

Print this page




  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++) {
  73                         for (;;) {
  74                             try { m.lock(); break; }
  75                             catch (Throwable t) { checkThrowable(t); }
  76                         }
  77 
  78                         try { check (! m.tryLock()); }
  79                         catch (Throwable t) { checkThrowable(t); }
  80 
  81                         try { check (! m.tryLock(1, TimeUnit.MICROSECONDS)); }
  82                         catch (Throwable t) { checkThrowable(t); }
  83 
  84                         m.unlock();
  85                     }
  86                 } catch (Throwable t) { unexpected(t); }}});}
  87         barrier.await();
  88         es.shutdown();
  89         check(es.awaitTermination(10, TimeUnit.SECONDS));
  90     }
  91 
  92     private static class FlakySync extends AbstractQueuedLongSynchronizer {
  93         private static final long serialVersionUID = -1L;
  94 
  95         public boolean isHeldExclusively() { return getState() == 1; }
  96 
  97         public boolean tryAcquire(long acquires) {
  98             // Sneak in some tests for queue state
  99             if (hasQueuedPredecessors())
 100                 check(getFirstQueuedThread() != Thread.currentThread());
 101             if (getFirstQueuedThread() == Thread.currentThread()) {




  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++) {
  73                         for (;;) {
  74                             try { m.lock(); break; }
  75                             catch (Throwable t) { checkThrowable(t); }
  76                         }
  77 
  78                         try { check(! m.tryLock()); }
  79                         catch (Throwable t) { checkThrowable(t); }
  80 
  81                         try { check(! m.tryLock(1, TimeUnit.MICROSECONDS)); }
  82                         catch (Throwable t) { checkThrowable(t); }
  83 
  84                         m.unlock();
  85                     }
  86                 } catch (Throwable t) { unexpected(t); }}});}
  87         barrier.await();
  88         es.shutdown();
  89         check(es.awaitTermination(10, TimeUnit.SECONDS));
  90     }
  91 
  92     private static class FlakySync extends AbstractQueuedLongSynchronizer {
  93         private static final long serialVersionUID = -1L;
  94 
  95         public boolean isHeldExclusively() { return getState() == 1; }
  96 
  97         public boolean tryAcquire(long acquires) {
  98             // Sneak in some tests for queue state
  99             if (hasQueuedPredecessors())
 100                 check(getFirstQueuedThread() != Thread.currentThread());
 101             if (getFirstQueuedThread() == Thread.currentThread()) {