ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ConcurrentDequeLoops.java
Revision: 1.9
Committed: Thu Apr 14 23:16:10 2011 UTC (13 years, 1 month ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.8: +1 -1 lines
Log Message:
whitespace

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 int k = 1;
47 for (int i = 1; i <= maxStages;) {
48 oneRun(klass, i, items);
49 if (i == k) {
50 k = i << 1;
51 i = i + (i >>> 1);
52 }
53 else
54 i = k;
55 }
56 pool.shutdown();
57 }
58
59 static class Stage implements Callable<Integer> {
60 final Deque<Integer> queue;
61 final CyclicBarrier barrier;
62 final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
63 int items;
64 Stage(Deque<Integer> q, CyclicBarrier b, int items) {
65 queue = q;
66 barrier = b;
67 this.items = items;
68 }
69
70 public Integer call() {
71 // Repeatedly take something from queue if possible,
72 // transform it, and put back in.
73 try {
74 barrier.await();
75 int l = (int) System.nanoTime();
76 int takes = 0;
77 for (;;) {
78 Integer item;
79 int rnd = rng.next();
80 if ((rnd & 1) == 0)
81 item = queue.pollFirst();
82 else
83 item = queue.pollLast();
84 if (item != null) {
85 ++takes;
86 l += LoopHelpers.compute2(item.intValue());
87 }
88 else if (takes != 0) {
89 totalItems.getAndAdd(-takes);
90 takes = 0;
91 }
92 else if (totalItems.get() <= 0)
93 break;
94 l = LoopHelpers.compute1(l);
95 if (items > 0) {
96 --items;
97 Integer res = new Integer(l);
98 if ((rnd & 16) == 0)
99 queue.addFirst(res);
100 else
101 queue.addLast(res);
102 }
103 else { // spinwait
104 for (int k = 1 + (l & 15); k != 0; --k)
105 l = LoopHelpers.compute1(LoopHelpers.compute2(l));
106 if ((l & 3) == 3) {
107 Thread.sleep(1);
108 }
109 }
110 }
111 return new Integer(l);
112 }
113 catch (Exception ie) {
114 ie.printStackTrace();
115 throw new Error("Call loop failed");
116 }
117 }
118 }
119
120 static void oneRun(Class klass, int n, int items) throws Exception {
121 Deque<Integer> q = (Deque<Integer>) klass.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 }
146 }