1 /* |
|
2 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. |
|
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 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6316155 6595669 |
|
27 * @summary Test concurrent offer vs. remove |
|
28 * @author Martin Buchholz |
|
29 */ |
|
30 |
|
31 import java.util.*; |
|
32 import java.util.concurrent.*; |
|
33 |
|
34 public class OfferRemoveLoops { |
|
35 void test(String[] args) throws Throwable { |
|
36 testQueue(new LinkedBlockingQueue<String>(10)); |
|
37 testQueue(new LinkedBlockingQueue<String>()); |
|
38 testQueue(new LinkedBlockingDeque<String>(10)); |
|
39 testQueue(new LinkedBlockingDeque<String>()); |
|
40 testQueue(new ArrayBlockingQueue<String>(10)); |
|
41 testQueue(new PriorityBlockingQueue<String>(10)); |
|
42 testQueue(new ConcurrentLinkedQueue<String>()); |
|
43 } |
|
44 |
|
45 abstract class CheckedThread extends Thread { |
|
46 abstract protected void realRun(); |
|
47 public void run() { |
|
48 try { realRun(); } catch (Throwable t) { unexpected(t); } |
|
49 } |
|
50 } |
|
51 |
|
52 void testQueue(final Queue<String> q) throws Throwable { |
|
53 System.out.println(q.getClass().getSimpleName()); |
|
54 final int count = 1000 * 1000; |
|
55 final long testDurationSeconds = 1L; |
|
56 final long testDurationMillis = testDurationSeconds * 1000L; |
|
57 final long quittingTimeNanos |
|
58 = System.nanoTime() + testDurationSeconds * 1000L * 1000L * 1000L; |
|
59 Thread t1 = new CheckedThread() { |
|
60 protected void realRun() { |
|
61 for (int i = 0; i < count; i++) { |
|
62 if ((i % 1024) == 0 && |
|
63 System.nanoTime() - quittingTimeNanos > 0) |
|
64 return; |
|
65 while (! q.remove(String.valueOf(i))) |
|
66 Thread.yield(); |
|
67 }}}; |
|
68 Thread t2 = new CheckedThread() { |
|
69 protected void realRun() { |
|
70 for (int i = 0; i < count; i++) { |
|
71 if ((i % 1024) == 0 && |
|
72 System.nanoTime() - quittingTimeNanos > 0) |
|
73 return; |
|
74 while (! q.offer(String.valueOf(i))) |
|
75 Thread.yield(); |
|
76 }}}; |
|
77 t1.setDaemon(true); t2.setDaemon(true); |
|
78 t1.start(); t2.start(); |
|
79 t1.join(10 * testDurationMillis); |
|
80 t2.join(10 * testDurationMillis); |
|
81 check(! t1.isAlive()); |
|
82 check(! t2.isAlive()); |
|
83 } |
|
84 |
|
85 //--------------------- Infrastructure --------------------------- |
|
86 volatile int passed = 0, failed = 0; |
|
87 void pass() {passed++;} |
|
88 void fail() {failed++; Thread.dumpStack();} |
|
89 void fail(String msg) {System.err.println(msg); fail();} |
|
90 void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
91 void check(boolean cond) {if (cond) pass(); else fail();} |
|
92 void equal(Object x, Object y) { |
|
93 if (x == null ? y == null : x.equals(y)) pass(); |
|
94 else fail(x + " not equal to " + y);} |
|
95 public static void main(String[] args) throws Throwable { |
|
96 new OfferRemoveLoops().instanceMain(args);} |
|
97 public void instanceMain(String[] args) throws Throwable { |
|
98 try {test(args);} catch (Throwable t) {unexpected(t);} |
|
99 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
100 if (failed > 0) throw new AssertionError("Some tests failed");} |
|
101 } |
|