/** compareAndSet in one thread enables another waiting for value to succeed */
  public void testCompareAndSetInMultipleThreads() throws Exception {
    x = one;
    final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
    try {
      a =
          AtomicReferenceFieldUpdater.newUpdater(
              AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
    } catch (RuntimeException ok) {
      return;
    }

    Thread t =
        new Thread(
            new CheckedRunnable() {
              public void realRun() {
                while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
                  Thread.yield();
              }
            });

    t.start();
    assertTrue(a.compareAndSet(this, one, two));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(a.get(this), three);
  }
예제 #2
0
 /**
  * Waits for the specified time (in milliseconds) for the thread to terminate (using {@link
  * Thread#join(long)}), else interrupts the thread (in the hope that it may terminate later) and
  * fails.
  */
 void awaitTermination(Thread t, long timeoutMillis) {
   try {
     t.join(timeoutMillis);
   } catch (InterruptedException ie) {
     threadUnexpectedException(ie);
   } finally {
     if (t.getState() != Thread.State.TERMINATED) {
       t.interrupt();
       fail("Test timed out");
     }
   }
 }
 /** compareAndSet in one thread enables another waiting for value to succeed */
 public void testCompareAndSetInMultipleThreads() {
   final AtomicLong ai = new AtomicLong(1);
   Thread t =
       new Thread(
           new Runnable() {
             public void run() {
               while (!ai.compareAndSet(2, 3)) Thread.yield();
             }
           });
   try {
     t.start();
     assertTrue(ai.compareAndSet(1, 2));
     t.join(LONG_DELAY_MS);
     assertFalse(t.isAlive());
     assertEquals(ai.get(), 3);
   } catch (Exception e) {
     unexpectedException();
   }
 }
예제 #4
0
  private void bug40333Simulation(final Region region, final String queryStr) throws Exception {
    final QueryService qs = CacheUtils.getQueryService();
    Region rgn = CacheUtils.getRegion("/new_pos");
    for (int i = 1; i < 100; ++i) {
      NewPortfolio pf = new NewPortfolio("name" + i, i);
      rgn.put("name" + i, pf);
    }
    final Object lock = new Object();
    final boolean[] expectionOccured = new boolean[] {false};
    final boolean[] keepGoing = new boolean[] {true};

    Thread indexCreatorDestroyer =
        new Thread(
            new Runnable() {
              public void run() {
                boolean continueRunning = true;
                do {

                  synchronized (lock) {
                    continueRunning = keepGoing[0];
                  }
                  try {
                    Index indx1 =
                        qs.createIndex(
                            "MarketValues",
                            IndexType.FUNCTIONAL,
                            "itr2.mktValue",
                            "/new_pos itr1, itr1.positions.values itr2");
                    Index indx2 =
                        qs.createIndex("Name", IndexType.FUNCTIONAL, "itr1.name", "/new_pos itr1");
                    Index indx3 =
                        qs.createIndex("nameIndex", IndexType.PRIMARY_KEY, "name", "/new_pos");
                    Index indx4 = qs.createIndex("idIndex", IndexType.FUNCTIONAL, "id", "/new_pos");
                    Index indx5 =
                        qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/new_pos");
                    Index indx6 =
                        qs.createIndex(
                            "undefinedFieldIndex",
                            IndexType.FUNCTIONAL,
                            "undefinedTestField.toString",
                            "/new_pos");
                    Thread.sleep(800);
                    qs.removeIndex(indx1);
                    qs.removeIndex(indx2);
                    qs.removeIndex(indx3);
                    qs.removeIndex(indx4);
                    qs.removeIndex(indx5);
                    qs.removeIndex(indx6);
                  } catch (Throwable e) {
                    region.getCache().getLogger().error(e);
                    e.printStackTrace();
                    synchronized (lock) {
                      expectionOccured[0] = true;
                      keepGoing[0] = false;
                      continueRunning = false;
                    }
                  }
                } while (continueRunning);
              }
            });

    indexCreatorDestroyer.start();
    final Query q = qs.newQuery(queryStr);
    final int THREAD_COUNT = 10;
    Thread queryThreads[] = new Thread[THREAD_COUNT];
    final int numTimesToRun = 75;
    for (int i = 0; i < THREAD_COUNT; ++i) {
      queryThreads[i] =
          new Thread(
              new Runnable() {
                public void run() {
                  boolean continueRunning = true;
                  for (int i = 0; i < numTimesToRun && continueRunning; ++i) {
                    synchronized (lock) {
                      continueRunning = keepGoing[0];
                    }
                    try {
                      SelectResults sr = (SelectResults) q.execute();
                    } catch (Throwable e) {
                      e.printStackTrace();
                      region.getCache().getLogger().error(e);
                      synchronized (lock) {
                        expectionOccured[0] = true;
                        keepGoing[0] = false;
                        continueRunning = false;
                      }
                      break;
                    }
                  }
                }
              });
    }
    synchronized (lock) {
      assertFalse(expectionOccured[0]);
    }

    for (int i = 0; i < THREAD_COUNT; ++i) {
      queryThreads[i].start();
    }

    for (int i = 0; i < THREAD_COUNT; ++i) {
      queryThreads[i].join();
    }
    synchronized (lock) {
      keepGoing[0] = false;
    }

    indexCreatorDestroyer.join();
    synchronized (lock) {
      assertFalse(expectionOccured[0]);
    }
  }