--- jsr166/src/test/loops/MapLoops.java 2009/11/03 01:04:02 1.6 +++ jsr166/src/test/loops/MapLoops.java 2016/10/23 03:03:23 1.13 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ import java.util.*; @@ -20,7 +20,7 @@ public class MapLoops { public static void main(String[] args) throws Exception { - Class mapClass = null; + Class mapClass = null; if (args.length > 0) { try { mapClass = Class.forName(args[0]); @@ -46,6 +46,9 @@ public class MapLoops { if (args.length > 5) nops = Integer.parseInt(args[5]); + if (nops < nkeys) + nops = nkeys; + // normalize probabilities wrt random number generator removesPerMaxRandom = (int)(((double)premove/100.0 * 0x7FFFFFFFL)); insertsPerMaxRandom = (int)(((double)pinsert/100.0 * 0x7FFFFFFFL)); @@ -58,23 +61,25 @@ public class MapLoops { System.out.print(" ops: " + nops); System.out.println(); - int k = 1; int warmups = 2; - for (int i = 1; i <= maxThreads;) { - Thread.sleep(100); - test(i, nkeys, mapClass); - if (warmups > 0) - --warmups; - else if (i == k) { - k = i << 1; - i = i + (i >>> 1); - } - else if (i == 1 && k == 2) { - i = k; - warmups = 1; + for (int reps = 0; reps < 3; ++reps) { + int k = 1; + for (int i = 1; i <= maxThreads;) { + Thread.sleep(100); + test(i, nkeys, mapClass); + if (warmups > 0) + --warmups; + else if (i == k) { + k = i << 1; + i = i + (i >>> 1); + } + else if (i == 1 && k == 2) { + i = k; + warmups = 1; + } + else + i = k; } - else - i = k; } pool.shutdown(); } @@ -97,9 +102,10 @@ public class MapLoops { } } - static void test(int i, int nkeys, Class mapClass) throws Exception { + static void test(int i, int nkeys, Class mapClass) throws Exception { System.out.print("Threads: " + i + "\t:"); - Map map = (Map)mapClass.newInstance(); + Map map = + (Map) mapClass.getConstructor().newInstance(); Integer[] key = makeKeys(nkeys); // Uncomment to start with a non-empty table // for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied @@ -137,7 +143,7 @@ public class MapLoops { } int step() { - // random-walk around key positions, bunching accesses + // random-walk around key positions, bunching accesses int r = rng.next(); position += (r & 7) - 3; while (position >= key.length) position -= key.length; @@ -159,7 +165,7 @@ public class MapLoops { } else if (r < insertsPerMaxRandom) { ++position; - map.put(k, k); + map.putIfAbsent(k, k); return 2; }