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

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
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 Class<?> klass = null;
22 if (args.length > 0) {
23 try {
24 klass = Class.forName(args[0]);
25 } catch (ClassNotFoundException e) {
26 throw new RuntimeException("Class " + args[0] + " not found.");
27 }
28 }
29 else
30 throw new Error();
31
32 if (args.length > 1)
33 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 for (int k = 1, i = 1; i <= maxStages;) {
47 oneRun(klass, i, items);
48 if (i == k) {
49 k = i << 1;
50 i = i + (i >>> 1);
51 }
52 else
53 i = k;
54 }
55 pool.shutdown();
56 }
57
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 Stage(Deque<Integer> q, CyclicBarrier b, int items) {
64 queue = q;
65 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 int l = (int) System.nanoTime();
75 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 else { // spinwait
103 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 catch (Exception ie) {
113 ie.printStackTrace();
114 throw new Error("Call loop failed");
115 }
116 }
117 }
118
119 static void oneRun(Class<?> klass, int n, int items) throws Exception {
120 Deque<Integer> q =
121 (Deque<Integer>) klass.getConstructor().newInstance();
122 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 for (int i = 0; i < n; ++i)
127 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 }