ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ConcurrentDequeLoops.java
Revision: 1.13
Committed: Sun Oct 23 03:03:23 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.12: +2 -1 lines
Log Message:
fix deprecation warnings for Class#newInstance

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.8 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     import java.util.*;
8     import java.util.concurrent.*;
9     import java.util.concurrent.atomic.*;
10     import java.util.concurrent.locks.*;
11    
12     public class ConcurrentDequeLoops {
13     static final ExecutorService pool = Executors.newCachedThreadPool();
14     static AtomicInteger totalItems;
15     static boolean print = false;
16    
17     public static void main(String[] args) throws Exception {
18     int maxStages = 8;
19     int items = 1000000;
20    
21 jsr166 1.11 Class<?> klass = null;
22 dl 1.1 if (args.length > 0) {
23     try {
24     klass = Class.forName(args[0]);
25 jsr166 1.4 } catch (ClassNotFoundException e) {
26 dl 1.1 throw new RuntimeException("Class " + args[0] + " not found.");
27     }
28     }
29 jsr166 1.3 else
30 dl 1.1 throw new Error();
31    
32 jsr166 1.3 if (args.length > 1)
33 dl 1.1 maxStages = Integer.parseInt(args[1]);
34    
35     System.out.print("Class: " + klass.getName());
36     System.out.println(" stages: " + maxStages);
37    
38     print = false;
39     System.out.println("Warmup...");
40     oneRun(klass, 1, items);
41     Thread.sleep(100);
42     oneRun(klass, 1, items);
43     Thread.sleep(100);
44     print = true;
45    
46 jsr166 1.10 for (int k = 1, i = 1; i <= maxStages;) {
47 dl 1.1 oneRun(klass, i, items);
48     if (i == k) {
49     k = i << 1;
50     i = i + (i >>> 1);
51 jsr166 1.3 }
52     else
53 dl 1.1 i = k;
54     }
55     pool.shutdown();
56 jsr166 1.9 }
57 dl 1.1
58     static class Stage implements Callable<Integer> {
59     final Deque<Integer> queue;
60     final CyclicBarrier barrier;
61     final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
62     int items;
63 jsr166 1.7 Stage(Deque<Integer> q, CyclicBarrier b, int items) {
64 jsr166 1.3 queue = q;
65 dl 1.1 barrier = b;
66     this.items = items;
67     }
68    
69     public Integer call() {
70     // Repeatedly take something from queue if possible,
71     // transform it, and put back in.
72     try {
73     barrier.await();
74 jsr166 1.6 int l = (int) System.nanoTime();
75 dl 1.1 int takes = 0;
76     for (;;) {
77     Integer item;
78     int rnd = rng.next();
79     if ((rnd & 1) == 0)
80     item = queue.pollFirst();
81     else
82     item = queue.pollLast();
83     if (item != null) {
84     ++takes;
85     l += LoopHelpers.compute2(item.intValue());
86     }
87     else if (takes != 0) {
88     totalItems.getAndAdd(-takes);
89     takes = 0;
90     }
91     else if (totalItems.get() <= 0)
92     break;
93     l = LoopHelpers.compute1(l);
94     if (items > 0) {
95     --items;
96     Integer res = new Integer(l);
97     if ((rnd & 16) == 0)
98     queue.addFirst(res);
99     else
100     queue.addLast(res);
101     }
102 jsr166 1.3 else { // spinwait
103 dl 1.1 for (int k = 1 + (l & 15); k != 0; --k)
104     l = LoopHelpers.compute1(LoopHelpers.compute2(l));
105     if ((l & 3) == 3) {
106     Thread.sleep(1);
107     }
108     }
109     }
110     return new Integer(l);
111     }
112 jsr166 1.3 catch (Exception ie) {
113 dl 1.1 ie.printStackTrace();
114     throw new Error("Call loop failed");
115     }
116     }
117     }
118    
119 jsr166 1.11 static void oneRun(Class<?> klass, int n, int items) throws Exception {
120 jsr166 1.13 Deque<Integer> q =
121     (Deque<Integer>) klass.getConstructor().newInstance();
122 dl 1.1 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
123     CyclicBarrier barrier = new CyclicBarrier(n + 1, timer);
124     totalItems = new AtomicInteger(n * items);
125     ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n);
126 jsr166 1.3 for (int i = 0; i < n; ++i)
127 dl 1.1 results.add(pool.submit(new Stage(q, barrier, items)));
128    
129     if (print)
130     System.out.print("Threads: " + n + "\t:");
131     barrier.await();
132     int total = 0;
133     for (int i = 0; i < n; ++i) {
134     Future<Integer> f = results.get(i);
135     Integer r = f.get();
136     total += r.intValue();
137     }
138     long endTime = System.nanoTime();
139     long time = endTime - timer.startTime;
140     if (print)
141     System.out.println(LoopHelpers.rightJustify(time / (items * n)) + " ns per item");
142     if (total == 0) // avoid overoptimization
143     System.out.println("useless result: " + total);
144     }
145     }