Exemplo n.º 1
0
 public void run() {
   synchronized (this) {
     while (!shouldStop && loops > 0) {
       lo.action1Direct(iloops, yieldMode);
       lo.action2Deferred(iloops, yieldMode);
       loops--;
     }
     stopped = true;
     this.notifyAll();
   }
 }
Exemplo n.º 2
0
  protected long testLockedObjectImpl(
      LockFactory.ImplType implType,
      boolean fair,
      int threadNum,
      int loops,
      int iloops,
      YieldMode yieldMode)
      throws InterruptedException {
    final long t0 = System.currentTimeMillis();
    LockedObject lo = new LockedObject(implType, fair);
    LockedObjectRunner[] runners = new LockedObjectRunner[threadNum];
    Thread[] threads = new Thread[threadNum];
    int i;

    for (i = 0; i < threadNum; i++) {
      runners[i] = new LockedObjectRunner1(lo, loops, iloops, yieldMode);
      // String name = Thread.currentThread().getName()+"-ActionThread-"+i+"_of_"+threadNum;
      String name = "ActionThread-" + i + "_of_" + threadNum;
      threads[i] = new Thread(runners[i], name);
      threads[i].start();
    }

    for (i = 0; i < threadNum; i++) {
      runners[i].waitUntilStopped();
    }
    while (0 < lo.getDeferredThreadCount()) {
      Thread.sleep(100);
    }
    Assert.assertEquals(0, lo.locker.getHoldCount());
    Assert.assertEquals(false, lo.locker.isLocked());
    Assert.assertEquals(0, lo.getDeferredThreadCount());

    final long dt = System.currentTimeMillis() - t0;
    lo.stats(false);

    System.err.println();
    final String fair_S = fair ? "fair  " : "unfair";
    System.err.printf(
        "---- TestRecursiveLock01.testLockedObjectThreading: i %5s, %s, threads %2d, loops-outter %6d, loops-inner %6d, yield %5s - dt %6d ms, avrg %8d ns, deviation [ %8d .. %8d ] ns",
        implType,
        fair_S,
        threadNum,
        loops,
        iloops,
        yieldMode,
        dt,
        lo.avrg,
        lo.min_deviation,
        lo.max_deviation);
    System.err.println();
    return dt;
  }