Esempio n. 1
0
  private static void runExecutionBoundedTest(int nthreads, List<Counter> counters, int nexecutions)
      throws InterruptedException {
    final List<CounterThread> threads = new ArrayList<CounterThread>();
    for (int i = 0; i < nthreads; i++) {
      CounterThread t;
      t = new CounterThread(counters.get(i % counters.size()), nexecutions);
      t.start();
      threads.add(t);
    }

    CounterThread.shoot(); // let all the threads go crazy at the same time

    for (int i = 0; i < nthreads; i++) {
      CounterThread t = threads.get(i);
      t.join(30000);
      if (t.isAlive()) {
        System.out.println("stuck thread name: " + t.toString());
        System.out.println("stuck thread state: " + t.getState());
        safe.ReentrantLock l = (safe.ReentrantLock) t.getLock();
        System.out.println("thread waiting lock: " + l);
        System.out.println("stuck thread increments: " + t.getExecutions());
        System.out.println("stuck thread counter value: " + t.getCounterCount());
        Thread other = l.getOwner();
        System.out.println("lock owner: " + other);
        if (other != null) {
          System.out.println("owner name: " + other.toString());
          System.out.println("state owner: " + other.getState());
        }
        // Keep program alive to dump thread stacks with: jstack -l $(pidof java)
        System.out.println(
            java.lang.management.ManagementFactory.getThreadMXBean().getPeakThreadCount());
        while (true) {}
      }
    }
    long sum = 0;
    for (int i = 0; i < counters.size(); ++i) {
      sum += counters.get(i).getCount();
    }
    System.out.println(sum);
    System.out.println(
        java.lang.management.ManagementFactory.getThreadMXBean().getPeakThreadCount());
  }