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