--- jsr166/src/test/loops/UnboundedQueueFillEmptyLoops.java 2014/12/18 18:43:22 1.9 +++ jsr166/src/test/loops/UnboundedQueueFillEmptyLoops.java 2016/10/29 06:37:34 1.14 @@ -4,10 +4,8 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import java.util.*; -import java.util.concurrent.*; -import java.util.concurrent.locks.*; -import java.util.concurrent.atomic.*; +import java.util.Random; +import java.util.Queue; public class UnboundedQueueFillEmptyLoops { static int maxSize = 10000; @@ -16,20 +14,23 @@ public class UnboundedQueueFillEmptyLoop static Integer[] numbers; public static void main(String[] args) throws Exception { - Class klass = null; - if (args.length > 0) { - try { - klass = Class.forName(args[0]); - } catch (ClassNotFoundException e) { - throw new RuntimeException("Class " + args[0] + " not found."); - } + if (args.length < 2) { + System.out.printf( + "Usage: UnboundedQueueFillEmptyLoops className [x maxSize]%n"); + System.exit(1); + } + + final Class klass; + try { + klass = Class.forName(args[0]); + } catch (ClassNotFoundException e) { + throw new RuntimeException("Class " + args[0] + " not found."); } if (args.length > 2) maxSize = Integer.parseInt(args[2]); - System.out.print("Class: " + klass.getName()); - System.out.println(" size: " + maxSize); + System.out.printf("Class: %s size: %d%n", klass.getName(), maxSize); numbers = new Integer[maxSize]; for (int i = 0; i < maxSize; ++i) @@ -45,7 +46,8 @@ public class UnboundedQueueFillEmptyLoop } static void oneRun(Class klass, int n) throws Exception { - Queue q = (Queue) klass.newInstance(); + Queue q = + (Queue) klass.getConstructor().newInstance(); int sum = total; int m = rng.nextInt(numbers.length); long startTime = System.nanoTime(); @@ -55,8 +57,7 @@ public class UnboundedQueueFillEmptyLoop m = 0; q.offer(numbers[m++]); } - Integer p; - while ((p = q.poll()) != null) + for (Integer p; (p = q.poll()) != null; ) sum += p.intValue(); } total += sum;