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

Bad multi-threaded performance of Object.hashCode

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • 10
    • 7
    • hotspot
    • x86
    • linux

      A DESCRIPTION OF THE REQUEST :
      I'm seeing a slow down of Object.hashCode with it taking over twice as long to compute the default hash code running 4 threads vs 1 thread for the same number of objects.

      JUSTIFICATION :
      Multi threading is becoming more common so this kind of thing is good to fix.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I'd hope it would take a similar amount of time doing this in parallel.
      ACTUAL -
      Run the attached test. You can change the number of threads. Each thread has the same amount of work to do so you'd hope that running 4 threads on a quad core machine might take about the same time as running a single thread. I'm seeing ~2.3 seconds for 4x but .9 s for 1x.

      ---------- BEGIN SOURCE ----------
      import java.util.Arrays;
      import java.util.List;
      import java.util.concurrent.Callable;
      import java.util.concurrent.ExecutorService;
      import java.util.concurrent.Executors;
      import java.util.concurrent.Future;
      import java.util.concurrent.ThreadFactory;


      public class ObjectHashCodePerformance {

        private static final int THREAD_COUNT = 4;
        private static final int ITERATIONS = 20000000;

        public static void main(final String[] args) throws Exception {
          long start = System.currentTimeMillis();
          new ObjectHashCodePerformance().run();
          System.err.println(System.currentTimeMillis() - start);
        }

        private final ExecutorService _sevice = Executors.newFixedThreadPool(THREAD_COUNT, new ThreadFactory() {
          private final ThreadFactory _delegate = Executors.defaultThreadFactory();
          @Override
          public Thread newThread(final Runnable r) {
            Thread thread = _delegate.newThread(r);
            thread.setDaemon(true);
            return thread;
          }
        });

        private void run() throws Exception {
          Callable<Void> work = new java.util.concurrent.Callable<Void>() {
            @Override
            public Void call() throws Exception {
              for (int i = 0; i < ITERATIONS; i++) {
                Object object = new Object();
                object.hashCode();
              }
              return null;
            }
          };
          @SuppressWarnings("unchecked")
          Callable<Void>[] allWork = new Callable[THREAD_COUNT];
          Arrays.fill(allWork, work);
          List<Future<Void>> futures = _sevice.invokeAll(Arrays.asList(allWork));
          for (Future<Void> future : futures) {
            future.get();
          }
        }

      }

      ---------- END SOURCE ----------

            dcubed Daniel Daugherty
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: