public static void main(String[] args) {
    // Create a thread pool with two threads
    ExecutorService executor = Executors.newFixedThreadPool(2);

    System.out.println("Thread 1\t\tThread 2\t\tBalance");

    executor.execute(new DepositTask());
    executor.execute(new WithDrawTask());
    executor.shutdown();
  }
示例#2
0
    final void test() throws Exception {
      Future[] futures = new Future[nthreads];
      for (int i = 0; i < nthreads; ++i) futures[i] = pool.submit(this);

      barrier.await();
      Thread.sleep(TIMEOUT);
      boolean tooLate = false;
      for (int i = 1; i < nthreads; ++i) {
        if (!futures[i].cancel(true)) tooLate = true;
        // Unbunch some of the cancels
        if ((i & 3) == 0) Thread.sleep(1 + rng.next() % 10);
      }

      Object f0 = futures[0].get();
      if (!tooLate) {
        for (int i = 1; i < nthreads; ++i) {
          if (!futures[i].isDone() || !futures[i].isCancelled())
            throw new Error("Only one thread should complete");
        }
      } else System.out.print("(cancelled too late) ");

      long endTime = System.nanoTime();
      long time = endTime - timer.startTime;
      if (print) {
        double secs = (double) (time) / 1000000000.0;
        System.out.println("\t " + secs + "s run time");
      }
    }
示例#3
0
 static Future<String> futureOutputOf(final InputStream is) {
   return drainers.submit(
       new Callable<String>() {
         public String call() throws IOException {
           return outputOf(is);
         }
       });
 }
示例#4
0
  public static void main(String[] args) throws Exception {
    int maxThreads = 5;
    if (args.length > 0) maxThreads = Integer.parseInt(args[0]);

    print = true;

    for (int i = 2; i <= maxThreads; i += (i + 1) >>> 1) {
      System.out.print("Threads: " + i);
      try {
        new FutureLoop(i).test();
      } catch (BrokenBarrierException bb) {
        // OK; ignore
      } catch (ExecutionException ee) {
        // OK; ignore
      }
      Thread.sleep(TIMEOUT);
    }
    pool.shutdown();
    if (!pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) throw new Error();
  }
示例#5
0
  static void realMain(String[] args) throws Throwable {
    // jmap doesn't work on Windows
    if (System.getProperty("os.name").startsWith("Windows")) return;

    final String childClassName = Job.class.getName();
    final String classToCheckForLeaks = Job.classToCheckForLeaks();
    final String uniqueID = String.valueOf(new Random().nextInt(Integer.MAX_VALUE));

    final String[] jobCmd = {
      java,
      "-Xmx8m",
      "-classpath",
      System.getProperty("test.classes", "."),
      childClassName,
      uniqueID
    };
    final Process p = new ProcessBuilder(jobCmd).start();

    final String childPid =
        match(
            commandOutputOf(jps, "-m"),
            "(?m)^ *([0-9]+) +\\Q" + childClassName + "\\E *" + uniqueID + "$",
            1);

    final int n0 = objectsInUse(p, childPid, classToCheckForLeaks);
    final int n1 = objectsInUse(p, childPid, classToCheckForLeaks);
    equal(p.waitFor(), 0);
    equal(p.exitValue(), 0);
    failed += p.exitValue();

    // Check that no objects were leaked.
    System.out.printf("%d -> %d%n", n0, n1);
    check(Math.abs(n1 - n0) < 2); // Almost always n0 == n1
    check(n1 < 20);
    drainers.shutdown();
  }