author | dl |
Mon, 02 Nov 2009 17:25:38 -0800 | |
changeset 4110 | ac033ba6ede4 |
parent 3708 | f838f712922e |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
19 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
20 |
* have any questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/licenses/publicdomain |
|
32 |
*/ |
|
33 |
||
34 |
/* |
|
35 |
* @test |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
36 |
* @bug 4486658 6785442 |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
37 |
* @run main ConcurrentQueueLoops 8 123456 |
2 | 38 |
* @summary Checks that a set of threads can repeatedly get and modify items |
39 |
*/ |
|
40 |
||
41 |
import java.util.*; |
|
42 |
import java.util.concurrent.*; |
|
43 |
import java.util.concurrent.atomic.*; |
|
44 |
||
45 |
public class ConcurrentQueueLoops { |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
46 |
ExecutorService pool; |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
47 |
AtomicInteger totalItems; |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
48 |
boolean print; |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
49 |
|
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
50 |
// Suitable for benchmarking. Overriden by args[0] for testing. |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
51 |
int maxStages = 20; |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
52 |
|
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
53 |
// Suitable for benchmarking. Overriden by args[1] for testing. |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
54 |
int items = 1024 * 1024; |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
55 |
|
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
56 |
Collection<Queue<Integer>> concurrentQueues() { |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
57 |
List<Queue<Integer>> queues = new ArrayList<Queue<Integer>>(); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
58 |
queues.add(new ConcurrentLinkedQueue<Integer>()); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
59 |
queues.add(new ArrayBlockingQueue<Integer>(items, false)); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
60 |
//queues.add(new ArrayBlockingQueue<Integer>(count, true)); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
61 |
queues.add(new LinkedBlockingQueue<Integer>()); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
62 |
queues.add(new LinkedBlockingDeque<Integer>()); |
4110 | 63 |
queues.add(new LinkedTransferQueue<Integer>()); |
2 | 64 |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
65 |
// Following additional implementations are available from: |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
66 |
// http://gee.cs.oswego.edu/dl/concurrency-interest/index.html |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
67 |
// queues.add(new SynchronizedLinkedListQueue<Integer>()); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
68 |
|
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
69 |
// Avoid "first fast, second slow" benchmark effect. |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
70 |
Collections.shuffle(queues); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
71 |
return queues; |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
72 |
} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
73 |
|
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
74 |
void test(String[] args) throws Throwable { |
2 | 75 |
if (args.length > 0) |
76 |
maxStages = Integer.parseInt(args[0]); |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
77 |
if (args.length > 1) |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
78 |
items = Integer.parseInt(args[1]); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
79 |
|
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
80 |
for (Queue<Integer> queue : concurrentQueues()) |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
81 |
test(queue); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
82 |
} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
83 |
|
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
84 |
void test(final Queue<Integer> q) throws Throwable { |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
85 |
System.out.println(q.getClass().getSimpleName()); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
86 |
pool = Executors.newCachedThreadPool(); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
87 |
print = false; |
2 | 88 |
|
89 |
print = false; |
|
90 |
System.out.println("Warmup..."); |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
91 |
oneRun(1, items, q); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
92 |
//Thread.sleep(100); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
93 |
oneRun(3, items, q); |
2 | 94 |
Thread.sleep(100); |
95 |
print = true; |
|
96 |
||
97 |
for (int i = 1; i <= maxStages; i += (i+1) >>> 1) { |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
98 |
oneRun(i, items, q); |
2 | 99 |
} |
100 |
pool.shutdown(); |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
101 |
check(pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)); |
2 | 102 |
} |
103 |
||
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
104 |
class Stage implements Callable<Integer> { |
2 | 105 |
final Queue<Integer> queue; |
106 |
final CyclicBarrier barrier; |
|
107 |
int items; |
|
108 |
Stage (Queue<Integer> q, CyclicBarrier b, int items) { |
|
109 |
queue = q; |
|
110 |
barrier = b; |
|
111 |
this.items = items; |
|
112 |
} |
|
113 |
||
114 |
public Integer call() { |
|
115 |
// Repeatedly take something from queue if possible, |
|
116 |
// transform it, and put back in. |
|
117 |
try { |
|
118 |
barrier.await(); |
|
119 |
int l = 4321; |
|
120 |
int takes = 0; |
|
121 |
for (;;) { |
|
122 |
Integer item = queue.poll(); |
|
123 |
if (item != null) { |
|
124 |
++takes; |
|
125 |
l = LoopHelpers.compute2(item.intValue()); |
|
126 |
} |
|
127 |
else if (takes != 0) { |
|
128 |
totalItems.getAndAdd(-takes); |
|
129 |
takes = 0; |
|
130 |
} |
|
131 |
else if (totalItems.get() <= 0) |
|
132 |
break; |
|
133 |
l = LoopHelpers.compute1(l); |
|
134 |
if (items > 0) { |
|
135 |
--items; |
|
136 |
queue.offer(new Integer(l)); |
|
137 |
} |
|
138 |
else if ( (l & (3 << 5)) == 0) // spinwait |
|
139 |
Thread.sleep(1); |
|
140 |
} |
|
141 |
return new Integer(l); |
|
142 |
} |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
143 |
catch (Throwable t) { unexpected(t); return null; } |
2 | 144 |
} |
145 |
} |
|
146 |
||
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
147 |
void oneRun(int n, int items, final Queue<Integer> q) throws Exception { |
2 | 148 |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
149 |
CyclicBarrier barrier = new CyclicBarrier(n + 1, timer); |
|
150 |
totalItems = new AtomicInteger(n * items); |
|
151 |
ArrayList<Future<Integer>> results = new ArrayList<Future<Integer>>(n); |
|
152 |
for (int i = 0; i < n; ++i) |
|
153 |
results.add(pool.submit(new Stage(q, barrier, items))); |
|
154 |
||
155 |
if (print) |
|
156 |
System.out.print("Threads: " + n + "\t:"); |
|
157 |
barrier.await(); |
|
158 |
int total = 0; |
|
159 |
for (int i = 0; i < n; ++i) { |
|
160 |
Future<Integer> f = results.get(i); |
|
161 |
Integer r = f.get(); |
|
162 |
total += r.intValue(); |
|
163 |
} |
|
164 |
long endTime = System.nanoTime(); |
|
165 |
long time = endTime - timer.startTime; |
|
166 |
if (print) |
|
167 |
System.out.println(LoopHelpers.rightJustify(time / (items * n)) + " ns per item"); |
|
168 |
if (total == 0) // avoid overoptimization |
|
169 |
System.out.println("useless result: " + total); |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
170 |
} |
2 | 171 |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
172 |
//--------------------- Infrastructure --------------------------- |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
173 |
volatile int passed = 0, failed = 0; |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
174 |
void pass() {passed++;} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
175 |
void fail() {failed++; Thread.dumpStack();} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
176 |
void fail(String msg) {System.err.println(msg); fail();} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
177 |
void unexpected(Throwable t) {failed++; t.printStackTrace();} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
178 |
void check(boolean cond) {if (cond) pass(); else fail();} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
179 |
void equal(Object x, Object y) { |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
180 |
if (x == null ? y == null : x.equals(y)) pass(); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
181 |
else fail(x + " not equal to " + y);} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
182 |
public static void main(String[] args) throws Throwable { |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
183 |
new ConcurrentQueueLoops().instanceMain(args);} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
184 |
public void instanceMain(String[] args) throws Throwable { |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
185 |
try {test(args);} catch (Throwable t) {unexpected(t);} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
186 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
2
diff
changeset
|
187 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
2 | 188 |
} |