1 |
|
/* |
2 |
|
* Written by Doug Lea and Martin Buchholz with assistance from members |
3 |
|
* of JCP JSR-166 Expert Group and released to the public domain, as |
4 |
< |
* explained at http://creativecommons.org/licenses/publicdomain |
4 |
> |
* explained at http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* |
6 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
7 |
|
* Pat Fisher, Mike Judd. |
71 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
72 |
|
} |
73 |
|
|
74 |
+ |
/** |
75 |
+ |
* take() blocks interruptibly when empty |
76 |
+ |
*/ |
77 |
+ |
public void testTakeFromEmptyBlocksInterruptibly() |
78 |
+ |
throws InterruptedException { |
79 |
+ |
final BlockingQueue q = emptyCollection(); |
80 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
81 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
82 |
+ |
public void realRun() { |
83 |
+ |
long t0 = System.nanoTime(); |
84 |
+ |
threadStarted.countDown(); |
85 |
+ |
try { |
86 |
+ |
q.take(); |
87 |
+ |
shouldThrow(); |
88 |
+ |
} catch (InterruptedException success) {} |
89 |
+ |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
90 |
+ |
}}); |
91 |
+ |
|
92 |
+ |
threadStarted.await(); |
93 |
+ |
delay(SHORT_DELAY_MS); |
94 |
+ |
assertTrue(t.isAlive()); |
95 |
+ |
t.interrupt(); |
96 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
97 |
+ |
} |
98 |
+ |
|
99 |
+ |
/** |
100 |
+ |
* take() throws InterruptedException immediately if interrupted |
101 |
+ |
* before waiting |
102 |
+ |
*/ |
103 |
+ |
public void testTakeFromEmptyAfterInterrupt() |
104 |
+ |
throws InterruptedException { |
105 |
+ |
final BlockingQueue q = emptyCollection(); |
106 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
107 |
+ |
public void realRun() { |
108 |
+ |
long t0 = System.nanoTime(); |
109 |
+ |
Thread.currentThread().interrupt(); |
110 |
+ |
try { |
111 |
+ |
q.take(); |
112 |
+ |
shouldThrow(); |
113 |
+ |
} catch (InterruptedException success) {} |
114 |
+ |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
115 |
+ |
}}); |
116 |
+ |
|
117 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
118 |
+ |
} |
119 |
+ |
|
120 |
|
/** For debugging. */ |
121 |
|
public void XXXXtestFails() { |
122 |
|
fail(emptyCollection().getClass().toString()); |