Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6384622

Exchanger.exchange(null) may hang

XMLWordPrintable

    • b75
    • generic, sparc
    • generic, solaris_9
    • Verified

      This test fails with

      Trial: 0
         2 threadsException in thread "main" java.lang.Error: Exchanger apparently stuck
      at NullExchanges.oneRun(NullExchanges.java:49)
      at NullExchanges.main(NullExchanges.java:24)
      ------------------------------------------------------------
      /*
       * Written by Bill Scherer and Doug Lea with assistance from members
       * of JCP JSR-166 Expert Group and released to the public domain. Use,
       * modify, and redistribute this code in any way without
       * acknowledgement.
       */

      import java.util.concurrent.*;
      import java.util.concurrent.atomic.*;
      import java.util.concurrent.locks.*;

      public class NullExchanges {
          static final int NCPUS = Runtime.getRuntime().availableProcessors();
          static final int DEFAULT_THREADS = NCPUS + 2;
          static final long DEFAULT_TRIAL_MILLIS = 10000;

          public static void main(String[] args) throws Exception {
              int maxThreads = DEFAULT_THREADS;
              long trialMillis = DEFAULT_TRIAL_MILLIS;
              int nReps = 3;
              for (int j = 0; j < nReps; ++j) {
                  System.out.println("Trial: " + j);
                  for (int i = 2; i <= maxThreads; i += 2) {
                      oneRun(i, trialMillis);
                  }
              }
          }

          static void oneRun(int nThreads, long trialMillis) throws Exception {
              System.out.printf("%4d threads", nThreads);
              Exchanger<Object> x = new Exchanger<Object>();
              CountDownLatch cd = new CountDownLatch(nThreads - 1);
              Runner[] runners = new Runner[nThreads];
              Thread[] threads = new Thread[nThreads];
              for (int i = 0; i < nThreads; ++i) {
                  runners[i] = new Runner(x, cd);
                  threads[i] = new Thread(runners[i]);
              }
              long startTime = System.nanoTime();
              for (int i = 0; i < nThreads; ++i) {
                  threads[i].start();
              }
              boolean ok = cd.await(10, TimeUnit.SECONDS);
              long elapsed = System.nanoTime() - startTime;
              for (int i = 0; i < nThreads; ++i) {
                  threads[i].interrupt();
              }
              if (!ok)
                  throw new Error("Exchanger apparently stuck");
              for (int i = 0; i < nThreads - 1; ++i)
                  threads[i].join();
              int iters = 1;
              for (int i = 0; i < nThreads; ++i) {
                  int ipr = runners[i].iters;
                  iters += ipr;
              }
              long rate = iters * 1000L * 1000L * 1000L / elapsed;
              long npt = elapsed / iters;
              System.out.printf("%9d it/s ", rate);
              System.out.printf("%9d ns/it", npt);
              System.out.println();
          }

          static final class Runner implements Runnable {
              final Exchanger<Object> exchanger;
              final CountDownLatch cd;
              final Object mine = new Integer(2688);
              volatile int iters;
              Runner(Exchanger<Object> x, CountDownLatch cd) {
                  this.exchanger = x;
                  this.cd = cd;
              }

              public void run() {
                  Exchanger<Object> x = exchanger;
                  Object m = null;
                  int i = 0;
                  try {
                      for (i = 0; i < 100000; ++i) {
                          m = x.exchange(m);
                      }
                  } catch (InterruptedException ie) {
                  } finally {
                      iters = i;
                      cd.countDown();
                  }
              }
          }
      }

            martin Martin Buchholz
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: