ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ConcurrentQueueLoops.java
Revision: 1.2
Committed: Tue Oct 4 20:09:41 2005 UTC (18 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.1: +62 -39 lines
Log Message:
Add collections tests

File Contents

# User Rev Content
1 dl 1.1 /*
2     * @test %I% %E%
3     * @bug 4486658
4     * @compile -source 1.5 ConcurrentQueueLoops.java
5     * @run main/timeout=230 ConcurrentQueueLoops
6     * @summary Checks that a set of threads can repeatedly get and modify items
7     */
8     /*
9     * Written by Doug Lea with assistance from members of JCP JSR-166
10     * Expert Group and released to the public domain. Use, modify, and
11     * redistribute this code in any way without acknowledgement.
12     */
13    
14     import java.util.*;
15     import java.util.concurrent.*;
16 dl 1.2 import java.util.concurrent.locks.*;
17 dl 1.1 import java.util.concurrent.atomic.*;
18    
19     public class ConcurrentQueueLoops {
20     static final ExecutorService pool = Executors.newCachedThreadPool();
21     static boolean print = false;
22 dl 1.2 static final Integer even = new Integer(42);
23     static final Integer odd = new Integer(17);
24     static int workMask;
25     static final long RUN_TIME_NANOS = 5 * 1000L * 1000L * 1000L;
26 dl 1.1
27     public static void main(String[] args) throws Exception {
28 dl 1.2 int maxStages = 48;
29     int work = 32;
30 dl 1.1 Class klass = null;
31     if (args.length > 0) {
32     try {
33     klass = Class.forName(args[0]);
34     } catch(ClassNotFoundException e) {
35     throw new RuntimeException("Class " + args[0] + " not found.");
36     }
37     }
38    
39     if (args.length > 1)
40     maxStages = Integer.parseInt(args[1]);
41    
42 dl 1.2 if (args.length > 2)
43     work = Integer.parseInt(args[2]);
44    
45     workMask = work - 1;
46 dl 1.1 System.out.print("Class: " + klass.getName());
47 dl 1.2 System.out.print(" stages: " + maxStages);
48     System.out.println(" work: " + work);
49 dl 1.1
50     print = false;
51     System.out.println("Warmup...");
52 dl 1.2 oneRun(klass, 4);
53 dl 1.1 Thread.sleep(100);
54 dl 1.2 oneRun(klass, 1);
55 dl 1.1 Thread.sleep(100);
56     print = true;
57    
58 dl 1.2 int k = 1;
59     for (int i = 1; i <= maxStages;) {
60     oneRun(klass, i);
61     if (i == k) {
62     k = i << 1;
63     i = i + (i >>> 1);
64     }
65     else
66     i = k;
67 dl 1.1 }
68     pool.shutdown();
69     }
70    
71 dl 1.2 static final class Stage implements Callable<Integer> {
72 dl 1.1 final Queue<Integer> queue;
73     final CyclicBarrier barrier;
74 dl 1.2 Stage (Queue<Integer> q, CyclicBarrier b) {
75 dl 1.1 queue = q;
76     barrier = b;
77 dl 1.2 }
78    
79     static int compute127(int l) {
80     l = LoopHelpers.compute1(l);
81     l = LoopHelpers.compute2(l);
82     l = LoopHelpers.compute3(l);
83     l = LoopHelpers.compute4(l);
84     l = LoopHelpers.compute5(l);
85     l = LoopHelpers.compute6(l);
86     return l;
87     }
88    
89     static int compute(int l) {
90     if (l == 0)
91     return (int)System.nanoTime();
92     int nn = l & workMask;
93     while (nn-- > 0)
94     l = compute127(l);
95     return l;
96 dl 1.1 }
97    
98     public Integer call() {
99     try {
100     barrier.await();
101 dl 1.2 long now = System.nanoTime();
102     long stopTime = now + RUN_TIME_NANOS;
103     int l = (int)now;
104 dl 1.1 int takes = 0;
105 dl 1.2 int misses = 0;
106 dl 1.1 for (;;) {
107 dl 1.2 l = compute(l);
108 dl 1.1 Integer item = queue.poll();
109     if (item != null) {
110 dl 1.2 l += item.intValue();
111 dl 1.1 ++takes;
112 dl 1.2 } else if ((misses++ & 255) == 0 &&
113     System.nanoTime() >= stopTime) {
114 dl 1.1 break;
115 dl 1.2 } else {
116     Integer a = ((l++ & 4)== 0)? even : odd;
117     queue.add(a);
118 dl 1.1 }
119     }
120 dl 1.2 return new Integer(takes);
121 dl 1.1 }
122     catch (Exception ie) {
123     ie.printStackTrace();
124     throw new Error("Call loop failed");
125     }
126     }
127     }
128    
129 dl 1.2 static void oneRun(Class klass, int n) throws Exception {
130 dl 1.1 Queue<Integer> q = (Queue<Integer>)klass.newInstance();
131     LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
132     CyclicBarrier barrier = new CyclicBarrier(n + 1, timer);
133     ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n);
134     for (int i = 0; i < n; ++i)
135 dl 1.2 results.add(pool.submit(new Stage(q, barrier)));
136 dl 1.1
137     if (print)
138     System.out.print("Threads: " + n + "\t:");
139     barrier.await();
140     int total = 0;
141     for (int i = 0; i < n; ++i) {
142     Future<Integer> f = results.get(i);
143     Integer r = f.get();
144     total += r.intValue();
145     }
146     long endTime = System.nanoTime();
147     long time = endTime - timer.startTime;
148 dl 1.2 long tpi = time / total;
149 dl 1.1 if (print)
150 dl 1.2 System.out.println(LoopHelpers.rightJustify(tpi) + " ns per item");
151 dl 1.1
152     }
153 dl 1.2
154 dl 1.1 }