예제 #1
0
  public void resetScore() {
    /*
     * score.set 与char2type.set这两个方法,虽然没有以原子的方式执行完成,但是
    却不影响整个类的安全性,因为这两个方法没有相互依赖,只需保证单个方法以原子的方式执行即可,
    这个情况与 java并发编程中的因式分解那个例子不同,
     */
    score.set(0);
    char2type.set(-1);

    setScore();
  }
  static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print)
      throws Exception {

    ThreadPoolExecutor pool =
        new ThreadPoolExecutor(nThreads + 1, Integer.MAX_VALUE, 1L, TimeUnit.SECONDS, q);

    CountDownLatch done = new CountDownLatch(iters);
    remaining.set(nThreads - 1);
    pool.prestartAllCoreThreads();
    Task t = new Task(pool, done);
    long start = System.nanoTime();
    pool.execute(t);
    done.await();
    long time = System.nanoTime() - start;
    if (print) System.out.println("\t: " + LoopHelpers.rightJustify(time / iters) + " ns per task");
    q.clear();
    Thread.sleep(100);
    pool.shutdown();
    Thread.sleep(100);
    pool.shutdownNow();
  }