author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 7668 | d4a77089c587 |
child 32991 | b27c76b82713 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
3708 | 26 |
* @bug 6316155 6595669 6871697 6868712 |
2 | 27 |
* @summary Test concurrent offer vs. remove |
3708 | 28 |
* @run main OfferRemoveLoops 300 |
2 | 29 |
* @author Martin Buchholz |
30 |
*/ |
|
31 |
||
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
32 |
import java.util.*; |
2 | 33 |
import java.util.concurrent.*; |
3708 | 34 |
import java.util.concurrent.atomic.*; |
2 | 35 |
|
3708 | 36 |
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) |
2 | 37 |
public class OfferRemoveLoops { |
3708 | 38 |
final long testDurationMillisDefault = 10L * 1000L; |
39 |
final long testDurationMillis; |
|
40 |
||
41 |
OfferRemoveLoops(String[] args) { |
|
42 |
testDurationMillis = (args.length > 0) ? |
|
43 |
Long.valueOf(args[0]) : testDurationMillisDefault; |
|
2 | 44 |
} |
45 |
||
3708 | 46 |
void checkNotContainsNull(Iterable it) { |
47 |
for (Object x : it) |
|
48 |
check(x != null); |
|
49 |
} |
|
50 |
||
51 |
void test(String[] args) throws Throwable { |
|
52 |
testQueue(new LinkedBlockingQueue(10)); |
|
53 |
testQueue(new LinkedBlockingQueue()); |
|
54 |
testQueue(new LinkedBlockingDeque(10)); |
|
55 |
testQueue(new LinkedBlockingDeque()); |
|
56 |
testQueue(new ArrayBlockingQueue(10)); |
|
57 |
testQueue(new PriorityBlockingQueue(10)); |
|
6672 | 58 |
testQueue(new ConcurrentLinkedDeque()); |
3708 | 59 |
testQueue(new ConcurrentLinkedQueue()); |
4110 | 60 |
testQueue(new LinkedTransferQueue()); |
3708 | 61 |
} |
62 |
||
63 |
Random getRandom() { |
|
4110 | 64 |
return ThreadLocalRandom.current(); |
2 | 65 |
} |
66 |
||
3708 | 67 |
void testQueue(final Queue q) throws Throwable { |
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
68 |
System.out.println(q.getClass().getSimpleName()); |
3708 | 69 |
final long testDurationNanos = testDurationMillis * 1000L * 1000L; |
70 |
final long quittingTimeNanos = System.nanoTime() + testDurationNanos; |
|
71 |
final long timeoutMillis = 10L * 1000L; |
|
72 |
final int maxChunkSize = 1042; |
|
73 |
final int maxQueueSize = 10 * maxChunkSize; |
|
74 |
||
75 |
/** Poor man's bounded buffer. */ |
|
76 |
final AtomicLong approximateCount = new AtomicLong(0L); |
|
77 |
||
78 |
abstract class CheckedThread extends Thread { |
|
79 |
CheckedThread(String name) { |
|
80 |
super(name); |
|
81 |
setDaemon(true); |
|
82 |
start(); |
|
83 |
} |
|
84 |
/** Polls for quitting time. */ |
|
85 |
protected boolean quittingTime() { |
|
86 |
return System.nanoTime() - quittingTimeNanos > 0; |
|
87 |
} |
|
88 |
/** Polls occasionally for quitting time. */ |
|
89 |
protected boolean quittingTime(long i) { |
|
90 |
return (i % 1024) == 0 && quittingTime(); |
|
91 |
} |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
7668
diff
changeset
|
92 |
protected abstract void realRun(); |
3708 | 93 |
public void run() { |
94 |
try { realRun(); } catch (Throwable t) { unexpected(t); } |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
Thread offerer = new CheckedThread("offerer") { |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
99 |
protected void realRun() { |
3708 | 100 |
final long chunkSize = getRandom().nextInt(maxChunkSize) + 2; |
101 |
long c = 0; |
|
102 |
for (long i = 0; ! quittingTime(i); i++) { |
|
103 |
if (q.offer(Long.valueOf(c))) { |
|
104 |
if ((++c % chunkSize) == 0) { |
|
105 |
approximateCount.getAndAdd(chunkSize); |
|
106 |
while (approximateCount.get() > maxQueueSize) |
|
107 |
Thread.yield(); |
|
108 |
} |
|
109 |
} else { |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
110 |
Thread.yield(); |
3708 | 111 |
}}}}; |
112 |
||
113 |
Thread remover = new CheckedThread("remover") { |
|
114 |
protected void realRun() { |
|
115 |
final long chunkSize = getRandom().nextInt(maxChunkSize) + 2; |
|
116 |
long c = 0; |
|
117 |
for (long i = 0; ! quittingTime(i); i++) { |
|
118 |
if (q.remove(Long.valueOf(c))) { |
|
119 |
if ((++c % chunkSize) == 0) { |
|
120 |
approximateCount.getAndAdd(-chunkSize); |
|
121 |
} |
|
122 |
} else { |
|
123 |
Thread.yield(); |
|
124 |
} |
|
125 |
} |
|
126 |
q.clear(); |
|
127 |
approximateCount.set(0); // Releases waiting offerer thread |
|
128 |
}}; |
|
129 |
||
130 |
Thread scanner = new CheckedThread("scanner") { |
|
131 |
protected void realRun() { |
|
132 |
final Random rnd = getRandom(); |
|
133 |
while (! quittingTime()) { |
|
134 |
switch (rnd.nextInt(3)) { |
|
135 |
case 0: checkNotContainsNull(q); break; |
|
136 |
case 1: q.size(); break; |
|
137 |
case 2: checkNotContainsNull |
|
138 |
(Arrays.asList(q.toArray(new Long[0]))); |
|
139 |
break; |
|
140 |
} |
|
141 |
Thread.yield(); |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
142 |
}}}; |
3708 | 143 |
|
144 |
for (Thread thread : new Thread[] { offerer, remover, scanner }) { |
|
145 |
thread.join(timeoutMillis + testDurationMillis); |
|
146 |
if (thread.isAlive()) { |
|
147 |
System.err.printf("Hung thread: %s%n", thread.getName()); |
|
148 |
failed++; |
|
149 |
for (StackTraceElement e : thread.getStackTrace()) |
|
150 |
System.err.println(e); |
|
151 |
// Kludge alert |
|
152 |
thread.stop(); |
|
153 |
thread.join(timeoutMillis); |
|
154 |
} |
|
155 |
} |
|
2 | 156 |
} |
157 |
||
158 |
//--------------------- Infrastructure --------------------------- |
|
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
159 |
volatile int passed = 0, failed = 0; |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
160 |
void pass() {passed++;} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
161 |
void fail() {failed++; Thread.dumpStack();} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
162 |
void fail(String msg) {System.err.println(msg); fail();} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
163 |
void unexpected(Throwable t) {failed++; t.printStackTrace();} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
164 |
void check(boolean cond) {if (cond) pass(); else fail();} |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
165 |
void equal(Object x, Object y) { |
2 | 166 |
if (x == null ? y == null : x.equals(y)) pass(); |
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
167 |
else fail(x + " not equal to " + y);} |
2 | 168 |
public static void main(String[] args) throws Throwable { |
3708 | 169 |
new OfferRemoveLoops(args).instanceMain(args);} |
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
170 |
public void instanceMain(String[] args) throws Throwable { |
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
171 |
try {test(args);} catch (Throwable t) {unexpected(t);} |
2 | 172 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
3414
cdf768813b4d
6785442: ConcurrentLinkedQueue.remove() and poll() can both remove the same element
dl
parents:
715
diff
changeset
|
173 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
2 | 174 |
} |