ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/UnboundedQueueFillEmptyLoops.java
Revision: 1.13
Committed: Sat Oct 29 06:23:09 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.12: +2 -4 lines
Log Message:
tidy imports

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.9 Class<?> klass = null;
18 dl 1.1 if (args.length > 0) {
19     try {
20     klass = Class.forName(args[0]);
21 jsr166 1.4 } catch (ClassNotFoundException e) {
22 dl 1.1 throw new RuntimeException("Class " + args[0] + " not found.");
23     }
24     }
25    
26 jsr166 1.3 if (args.length > 2)
27 dl 1.1 maxSize = Integer.parseInt(args[2]);
28    
29     System.out.print("Class: " + klass.getName());
30     System.out.println(" size: " + maxSize);
31    
32     numbers = new Integer[maxSize];
33     for (int i = 0; i < maxSize; ++i)
34     numbers[i] = rng.nextInt(128);
35    
36     oneRun(klass, maxSize);
37     Thread.sleep(100);
38     oneRun(klass, maxSize);
39     Thread.sleep(100);
40     oneRun(klass, maxSize);
41    
42     if (total == 0) System.out.print(" ");
43 jsr166 1.8 }
44 dl 1.1
45 jsr166 1.9 static void oneRun(Class<?> klass, int n) throws Exception {
46 jsr166 1.12 Queue<Integer> q =
47     (Queue<Integer>) klass.getConstructor().newInstance();
48 dl 1.1 int sum = total;
49     int m = rng.nextInt(numbers.length);
50     long startTime = System.nanoTime();
51     for (int k = 0; k < n; ++k) {
52     for (int i = 0; i < k; ++i) {
53     if (m >= numbers.length)
54     m = 0;
55     q.offer(numbers[m++]);
56     }
57 jsr166 1.10 for (Integer p; (p = q.poll()) != null; )
58 dl 1.1 sum += p.intValue();
59     }
60     total += sum;
61     long endTime = System.nanoTime();
62     long time = endTime - startTime;
63 jsr166 1.6 double secs = (double) time / 1000000000.0;
64 dl 1.1 System.out.println("Time: " + secs);
65     }
66    
67     }