ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/UnboundedQueueFillEmptyLoops.java
Revision: 1.14
Committed: Sat Oct 29 06:37:34 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.13: +12 -9 lines
Log Message:
cosmetic improvements

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.2 * Expert Group and released to the public domain, as explained at
4 jsr166 1.7 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7 jsr166 1.13 import java.util.Random;
8     import java.util.Queue;
9 dl 1.1
10     public class UnboundedQueueFillEmptyLoops {
11     static int maxSize = 10000;
12     static Random rng = new Random(3153122688L);
13     static volatile int total;
14     static Integer[] numbers;
15    
16     public static void main(String[] args) throws Exception {
17 jsr166 1.14 if (args.length < 2) {
18     System.out.printf(
19     "Usage: UnboundedQueueFillEmptyLoops className [x maxSize]%n");
20     System.exit(1);
21     }
22    
23     final Class<?> klass;
24     try {
25     klass = Class.forName(args[0]);
26     } catch (ClassNotFoundException e) {
27     throw new RuntimeException("Class " + args[0] + " not found.");
28 dl 1.1 }
29    
30 jsr166 1.3 if (args.length > 2)
31 dl 1.1 maxSize = Integer.parseInt(args[2]);
32    
33 jsr166 1.14 System.out.printf("Class: %s size: %d%n", klass.getName(), maxSize);
34 dl 1.1
35     numbers = new Integer[maxSize];
36     for (int i = 0; i < maxSize; ++i)
37     numbers[i] = rng.nextInt(128);
38    
39     oneRun(klass, maxSize);
40     Thread.sleep(100);
41     oneRun(klass, maxSize);
42     Thread.sleep(100);
43     oneRun(klass, maxSize);
44    
45     if (total == 0) System.out.print(" ");
46 jsr166 1.8 }
47 dl 1.1
48 jsr166 1.9 static void oneRun(Class<?> klass, int n) throws Exception {
49 jsr166 1.12 Queue<Integer> q =
50     (Queue<Integer>) klass.getConstructor().newInstance();
51 dl 1.1 int sum = total;
52     int m = rng.nextInt(numbers.length);
53     long startTime = System.nanoTime();
54     for (int k = 0; k < n; ++k) {
55     for (int i = 0; i < k; ++i) {
56     if (m >= numbers.length)
57     m = 0;
58     q.offer(numbers[m++]);
59     }
60 jsr166 1.10 for (Integer p; (p = q.poll()) != null; )
61 dl 1.1 sum += p.intValue();
62     }
63     total += sum;
64     long endTime = System.nanoTime();
65     long time = endTime - startTime;
66 jsr166 1.6 double secs = (double) time / 1000000000.0;
67 dl 1.1 System.out.println("Time: " + secs);
68     }
69    
70     }