1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.util.concurrent.locks.*; |
15 |
|
import java.io.*; |
16 |
|
|
17 |
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
33 |
|
static class Mutex extends AbstractQueuedLongSynchronizer { |
34 |
|
// Use value > 32 bits for locked state |
35 |
|
static final long LOCKED = 1 << 48; |
36 |
< |
public boolean isHeldExclusively() { |
37 |
< |
return getState() == LOCKED; |
36 |
> |
public boolean isHeldExclusively() { |
37 |
> |
return getState() == LOCKED; |
38 |
|
} |
39 |
< |
|
39 |
> |
|
40 |
|
public boolean tryAcquire(long acquires) { |
41 |
|
return compareAndSetState(0, LOCKED); |
42 |
|
} |
43 |
< |
|
43 |
> |
|
44 |
|
public boolean tryRelease(long releases) { |
45 |
|
if (getState() == 0) throw new IllegalMonitorStateException(); |
46 |
|
setState(0); |
47 |
|
return true; |
48 |
|
} |
49 |
< |
|
50 |
< |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { return new AbstractQueuedLongSynchronizer.ConditionObject(); } |
49 |
> |
|
50 |
> |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { |
51 |
> |
return new AbstractQueuedLongSynchronizer.ConditionObject(); |
52 |
> |
} |
53 |
|
|
54 |
|
} |
55 |
|
|
56 |
< |
|
56 |
> |
|
57 |
|
/** |
58 |
|
* A simple latch class, to test shared mode. |
59 |
|
*/ |
60 |
< |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
60 |
> |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
61 |
|
public boolean isSignalled() { return getState() != 0; } |
62 |
|
|
63 |
|
public long tryAcquireShared(long ignore) { |
64 |
< |
return isSignalled()? 1 : -1; |
64 |
> |
return isSignalled() ? 1 : -1; |
65 |
|
} |
66 |
< |
|
66 |
> |
|
67 |
|
public boolean tryReleaseShared(long ignore) { |
68 |
|
setState(1 << 62); |
69 |
|
return true; |
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
< |
* A runnable calling acquireInterruptibly |
74 |
> |
* A runnable calling acquireInterruptibly that does not expect to |
75 |
> |
* be interrupted. |
76 |
|
*/ |
77 |
< |
class InterruptibleSyncRunnable implements Runnable { |
77 |
> |
class InterruptibleSyncRunnable extends CheckedRunnable { |
78 |
|
final Mutex sync; |
79 |
|
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
80 |
< |
public void run() { |
81 |
< |
try { |
78 |
< |
sync.acquireInterruptibly(1); |
79 |
< |
} catch(InterruptedException success){} |
80 |
> |
public void realRun() throws InterruptedException { |
81 |
> |
sync.acquireInterruptibly(1); |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
/** |
87 |
|
* A runnable calling acquireInterruptibly that expects to be |
88 |
< |
* interrupted |
88 |
> |
* interrupted. |
89 |
|
*/ |
90 |
< |
class InterruptedSyncRunnable implements Runnable { |
90 |
> |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
91 |
|
final Mutex sync; |
92 |
|
InterruptedSyncRunnable(Mutex l) { sync = l; } |
93 |
< |
public void run() { |
94 |
< |
try { |
93 |
< |
sync.acquireInterruptibly(1); |
94 |
< |
threadShouldThrow(); |
95 |
< |
} catch(InterruptedException success){} |
93 |
> |
public void realRun() throws InterruptedException { |
94 |
> |
sync.acquireInterruptibly(1); |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
|
* isHeldExclusively is false upon construction |
100 |
|
*/ |
101 |
< |
public void testIsHeldExclusively() { |
102 |
< |
Mutex rl = new Mutex(); |
101 |
> |
public void testIsHeldExclusively() { |
102 |
> |
Mutex rl = new Mutex(); |
103 |
|
assertFalse(rl.isHeldExclusively()); |
104 |
|
} |
105 |
< |
|
105 |
> |
|
106 |
|
/** |
107 |
|
* acquiring released sync succeeds |
108 |
|
*/ |
109 |
< |
public void testAcquire() { |
110 |
< |
Mutex rl = new Mutex(); |
109 |
> |
public void testAcquire() { |
110 |
> |
Mutex rl = new Mutex(); |
111 |
|
rl.acquire(1); |
112 |
|
assertTrue(rl.isHeldExclusively()); |
113 |
|
rl.release(1); |
117 |
|
/** |
118 |
|
* tryAcquire on an released sync succeeds |
119 |
|
*/ |
120 |
< |
public void testTryAcquire() { |
121 |
< |
Mutex rl = new Mutex(); |
120 |
> |
public void testTryAcquire() { |
121 |
> |
Mutex rl = new Mutex(); |
122 |
|
assertTrue(rl.tryAcquire(1)); |
123 |
|
assertTrue(rl.isHeldExclusively()); |
124 |
|
rl.release(1); |
127 |
|
/** |
128 |
|
* hasQueuedThreads reports whether there are waiting threads |
129 |
|
*/ |
130 |
< |
public void testhasQueuedThreads() { |
131 |
< |
final Mutex sync = new Mutex(); |
130 |
> |
public void testhasQueuedThreads() throws InterruptedException { |
131 |
> |
final Mutex sync = new Mutex(); |
132 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
133 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
134 |
< |
try { |
135 |
< |
assertFalse(sync.hasQueuedThreads()); |
136 |
< |
sync.acquire(1); |
137 |
< |
t1.start(); |
138 |
< |
Thread.sleep(SHORT_DELAY_MS); |
139 |
< |
assertTrue(sync.hasQueuedThreads()); |
140 |
< |
t2.start(); |
141 |
< |
Thread.sleep(SHORT_DELAY_MS); |
142 |
< |
assertTrue(sync.hasQueuedThreads()); |
143 |
< |
t1.interrupt(); |
144 |
< |
Thread.sleep(SHORT_DELAY_MS); |
145 |
< |
assertTrue(sync.hasQueuedThreads()); |
146 |
< |
sync.release(1); |
147 |
< |
Thread.sleep(SHORT_DELAY_MS); |
148 |
< |
assertFalse(sync.hasQueuedThreads()); |
149 |
< |
t1.join(); |
150 |
< |
t2.join(); |
152 |
< |
} catch(Exception e){ |
153 |
< |
unexpectedException(); |
154 |
< |
} |
155 |
< |
} |
134 |
> |
assertFalse(sync.hasQueuedThreads()); |
135 |
> |
sync.acquire(1); |
136 |
> |
t1.start(); |
137 |
> |
delay(SHORT_DELAY_MS); |
138 |
> |
assertTrue(sync.hasQueuedThreads()); |
139 |
> |
t2.start(); |
140 |
> |
delay(SHORT_DELAY_MS); |
141 |
> |
assertTrue(sync.hasQueuedThreads()); |
142 |
> |
t1.interrupt(); |
143 |
> |
delay(SHORT_DELAY_MS); |
144 |
> |
assertTrue(sync.hasQueuedThreads()); |
145 |
> |
sync.release(1); |
146 |
> |
delay(SHORT_DELAY_MS); |
147 |
> |
assertFalse(sync.hasQueuedThreads()); |
148 |
> |
t1.join(); |
149 |
> |
t2.join(); |
150 |
> |
} |
151 |
|
|
152 |
|
/** |
153 |
|
* isQueued(null) throws NPE |
154 |
|
*/ |
155 |
< |
public void testIsQueuedNPE() { |
156 |
< |
final Mutex sync = new Mutex(); |
155 |
> |
public void testIsQueuedNPE() { |
156 |
> |
final Mutex sync = new Mutex(); |
157 |
|
try { |
158 |
|
sync.isQueued(null); |
159 |
|
shouldThrow(); |
160 |
< |
} catch (NullPointerException success) { |
166 |
< |
} |
160 |
> |
} catch (NullPointerException success) {} |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
|
* isQueued reports whether a thread is queued. |
165 |
|
*/ |
166 |
< |
public void testIsQueued() { |
167 |
< |
final Mutex sync = new Mutex(); |
166 |
> |
public void testIsQueued() throws InterruptedException { |
167 |
> |
final Mutex sync = new Mutex(); |
168 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
169 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
170 |
< |
try { |
171 |
< |
assertFalse(sync.isQueued(t1)); |
172 |
< |
assertFalse(sync.isQueued(t2)); |
173 |
< |
sync.acquire(1); |
174 |
< |
t1.start(); |
175 |
< |
Thread.sleep(SHORT_DELAY_MS); |
176 |
< |
assertTrue(sync.isQueued(t1)); |
177 |
< |
t2.start(); |
178 |
< |
Thread.sleep(SHORT_DELAY_MS); |
179 |
< |
assertTrue(sync.isQueued(t1)); |
180 |
< |
assertTrue(sync.isQueued(t2)); |
181 |
< |
t1.interrupt(); |
182 |
< |
Thread.sleep(SHORT_DELAY_MS); |
183 |
< |
assertFalse(sync.isQueued(t1)); |
184 |
< |
assertTrue(sync.isQueued(t2)); |
185 |
< |
sync.release(1); |
186 |
< |
Thread.sleep(SHORT_DELAY_MS); |
187 |
< |
assertFalse(sync.isQueued(t1)); |
188 |
< |
Thread.sleep(SHORT_DELAY_MS); |
189 |
< |
assertFalse(sync.isQueued(t2)); |
190 |
< |
t1.join(); |
191 |
< |
t2.join(); |
198 |
< |
} catch(Exception e){ |
199 |
< |
unexpectedException(); |
200 |
< |
} |
201 |
< |
} |
170 |
> |
assertFalse(sync.isQueued(t1)); |
171 |
> |
assertFalse(sync.isQueued(t2)); |
172 |
> |
sync.acquire(1); |
173 |
> |
t1.start(); |
174 |
> |
delay(SHORT_DELAY_MS); |
175 |
> |
assertTrue(sync.isQueued(t1)); |
176 |
> |
t2.start(); |
177 |
> |
delay(SHORT_DELAY_MS); |
178 |
> |
assertTrue(sync.isQueued(t1)); |
179 |
> |
assertTrue(sync.isQueued(t2)); |
180 |
> |
t1.interrupt(); |
181 |
> |
delay(SHORT_DELAY_MS); |
182 |
> |
assertFalse(sync.isQueued(t1)); |
183 |
> |
assertTrue(sync.isQueued(t2)); |
184 |
> |
sync.release(1); |
185 |
> |
delay(SHORT_DELAY_MS); |
186 |
> |
assertFalse(sync.isQueued(t1)); |
187 |
> |
delay(SHORT_DELAY_MS); |
188 |
> |
assertFalse(sync.isQueued(t2)); |
189 |
> |
t1.join(); |
190 |
> |
t2.join(); |
191 |
> |
} |
192 |
|
|
193 |
|
/** |
194 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
195 |
|
*/ |
196 |
< |
public void testGetFirstQueuedThread() { |
197 |
< |
final Mutex sync = new Mutex(); |
196 |
> |
public void testGetFirstQueuedThread() throws InterruptedException { |
197 |
> |
final Mutex sync = new Mutex(); |
198 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
199 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
200 |
< |
try { |
201 |
< |
assertNull(sync.getFirstQueuedThread()); |
202 |
< |
sync.acquire(1); |
203 |
< |
t1.start(); |
204 |
< |
Thread.sleep(SHORT_DELAY_MS); |
205 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
206 |
< |
t2.start(); |
207 |
< |
Thread.sleep(SHORT_DELAY_MS); |
208 |
< |
assertEquals(t1, sync.getFirstQueuedThread()); |
209 |
< |
t1.interrupt(); |
210 |
< |
Thread.sleep(SHORT_DELAY_MS); |
211 |
< |
Thread.sleep(SHORT_DELAY_MS); |
212 |
< |
assertEquals(t2, sync.getFirstQueuedThread()); |
213 |
< |
sync.release(1); |
214 |
< |
Thread.sleep(SHORT_DELAY_MS); |
215 |
< |
assertNull(sync.getFirstQueuedThread()); |
216 |
< |
t1.join(); |
217 |
< |
t2.join(); |
228 |
< |
} catch(Exception e){ |
229 |
< |
unexpectedException(); |
230 |
< |
} |
231 |
< |
} |
200 |
> |
assertNull(sync.getFirstQueuedThread()); |
201 |
> |
sync.acquire(1); |
202 |
> |
t1.start(); |
203 |
> |
delay(SHORT_DELAY_MS); |
204 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
205 |
> |
t2.start(); |
206 |
> |
delay(SHORT_DELAY_MS); |
207 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
208 |
> |
t1.interrupt(); |
209 |
> |
delay(SHORT_DELAY_MS); |
210 |
> |
delay(SHORT_DELAY_MS); |
211 |
> |
assertEquals(t2, sync.getFirstQueuedThread()); |
212 |
> |
sync.release(1); |
213 |
> |
delay(SHORT_DELAY_MS); |
214 |
> |
assertNull(sync.getFirstQueuedThread()); |
215 |
> |
t1.join(); |
216 |
> |
t2.join(); |
217 |
> |
} |
218 |
|
|
219 |
|
|
220 |
|
/** |
221 |
|
* hasContended reports false if no thread has ever blocked, else true |
222 |
|
*/ |
223 |
< |
public void testHasContended() { |
224 |
< |
final Mutex sync = new Mutex(); |
223 |
> |
public void testHasContended() throws InterruptedException { |
224 |
> |
final Mutex sync = new Mutex(); |
225 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
226 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
227 |
< |
try { |
228 |
< |
assertFalse(sync.hasContended()); |
229 |
< |
sync.acquire(1); |
230 |
< |
t1.start(); |
231 |
< |
Thread.sleep(SHORT_DELAY_MS); |
232 |
< |
assertTrue(sync.hasContended()); |
233 |
< |
t2.start(); |
234 |
< |
Thread.sleep(SHORT_DELAY_MS); |
235 |
< |
assertTrue(sync.hasContended()); |
236 |
< |
t1.interrupt(); |
237 |
< |
Thread.sleep(SHORT_DELAY_MS); |
238 |
< |
assertTrue(sync.hasContended()); |
239 |
< |
sync.release(1); |
240 |
< |
Thread.sleep(SHORT_DELAY_MS); |
241 |
< |
assertTrue(sync.hasContended()); |
242 |
< |
t1.join(); |
243 |
< |
t2.join(); |
258 |
< |
} catch(Exception e){ |
259 |
< |
unexpectedException(); |
260 |
< |
} |
261 |
< |
} |
227 |
> |
assertFalse(sync.hasContended()); |
228 |
> |
sync.acquire(1); |
229 |
> |
t1.start(); |
230 |
> |
delay(SHORT_DELAY_MS); |
231 |
> |
assertTrue(sync.hasContended()); |
232 |
> |
t2.start(); |
233 |
> |
delay(SHORT_DELAY_MS); |
234 |
> |
assertTrue(sync.hasContended()); |
235 |
> |
t1.interrupt(); |
236 |
> |
delay(SHORT_DELAY_MS); |
237 |
> |
assertTrue(sync.hasContended()); |
238 |
> |
sync.release(1); |
239 |
> |
delay(SHORT_DELAY_MS); |
240 |
> |
assertTrue(sync.hasContended()); |
241 |
> |
t1.join(); |
242 |
> |
t2.join(); |
243 |
> |
} |
244 |
|
|
245 |
|
/** |
246 |
|
* getQueuedThreads includes waiting threads |
247 |
|
*/ |
248 |
< |
public void testGetQueuedThreads() { |
249 |
< |
final Mutex sync = new Mutex(); |
248 |
> |
public void testGetQueuedThreads() throws InterruptedException { |
249 |
> |
final Mutex sync = new Mutex(); |
250 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
251 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
252 |
< |
try { |
253 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
254 |
< |
sync.acquire(1); |
255 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
256 |
< |
t1.start(); |
257 |
< |
Thread.sleep(SHORT_DELAY_MS); |
258 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
259 |
< |
t2.start(); |
260 |
< |
Thread.sleep(SHORT_DELAY_MS); |
261 |
< |
assertTrue(sync.getQueuedThreads().contains(t1)); |
262 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
263 |
< |
t1.interrupt(); |
264 |
< |
Thread.sleep(SHORT_DELAY_MS); |
265 |
< |
assertFalse(sync.getQueuedThreads().contains(t1)); |
266 |
< |
assertTrue(sync.getQueuedThreads().contains(t2)); |
267 |
< |
sync.release(1); |
268 |
< |
Thread.sleep(SHORT_DELAY_MS); |
269 |
< |
assertTrue(sync.getQueuedThreads().isEmpty()); |
270 |
< |
t1.join(); |
271 |
< |
t2.join(); |
290 |
< |
} catch(Exception e){ |
291 |
< |
unexpectedException(); |
292 |
< |
} |
293 |
< |
} |
252 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
253 |
> |
sync.acquire(1); |
254 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
255 |
> |
t1.start(); |
256 |
> |
delay(SHORT_DELAY_MS); |
257 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
258 |
> |
t2.start(); |
259 |
> |
delay(SHORT_DELAY_MS); |
260 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
261 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
262 |
> |
t1.interrupt(); |
263 |
> |
delay(SHORT_DELAY_MS); |
264 |
> |
assertFalse(sync.getQueuedThreads().contains(t1)); |
265 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
266 |
> |
sync.release(1); |
267 |
> |
delay(SHORT_DELAY_MS); |
268 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
269 |
> |
t1.join(); |
270 |
> |
t2.join(); |
271 |
> |
} |
272 |
|
|
273 |
|
/** |
274 |
|
* getExclusiveQueuedThreads includes waiting threads |
275 |
|
*/ |
276 |
< |
public void testGetExclusiveQueuedThreads() { |
277 |
< |
final Mutex sync = new Mutex(); |
276 |
> |
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
277 |
> |
final Mutex sync = new Mutex(); |
278 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
279 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
280 |
< |
try { |
281 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
282 |
< |
sync.acquire(1); |
283 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
284 |
< |
t1.start(); |
285 |
< |
Thread.sleep(SHORT_DELAY_MS); |
286 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
287 |
< |
t2.start(); |
288 |
< |
Thread.sleep(SHORT_DELAY_MS); |
289 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
290 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
291 |
< |
t1.interrupt(); |
292 |
< |
Thread.sleep(SHORT_DELAY_MS); |
293 |
< |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
294 |
< |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
295 |
< |
sync.release(1); |
296 |
< |
Thread.sleep(SHORT_DELAY_MS); |
297 |
< |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
298 |
< |
t1.join(); |
299 |
< |
t2.join(); |
322 |
< |
} catch(Exception e){ |
323 |
< |
unexpectedException(); |
324 |
< |
} |
325 |
< |
} |
280 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
281 |
> |
sync.acquire(1); |
282 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
283 |
> |
t1.start(); |
284 |
> |
delay(SHORT_DELAY_MS); |
285 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
286 |
> |
t2.start(); |
287 |
> |
delay(SHORT_DELAY_MS); |
288 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
289 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
290 |
> |
t1.interrupt(); |
291 |
> |
delay(SHORT_DELAY_MS); |
292 |
> |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
293 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
294 |
> |
sync.release(1); |
295 |
> |
delay(SHORT_DELAY_MS); |
296 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
297 |
> |
t1.join(); |
298 |
> |
t2.join(); |
299 |
> |
} |
300 |
|
|
301 |
|
/** |
302 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
303 |
|
*/ |
304 |
< |
public void testGetSharedQueuedThreads() { |
305 |
< |
final Mutex sync = new Mutex(); |
304 |
> |
public void testGetSharedQueuedThreads() throws InterruptedException { |
305 |
> |
final Mutex sync = new Mutex(); |
306 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
307 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
308 |
< |
try { |
309 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
310 |
< |
sync.acquire(1); |
311 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
312 |
< |
t1.start(); |
313 |
< |
Thread.sleep(SHORT_DELAY_MS); |
314 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
315 |
< |
t2.start(); |
316 |
< |
Thread.sleep(SHORT_DELAY_MS); |
317 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
318 |
< |
t1.interrupt(); |
319 |
< |
Thread.sleep(SHORT_DELAY_MS); |
320 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
321 |
< |
sync.release(1); |
322 |
< |
Thread.sleep(SHORT_DELAY_MS); |
323 |
< |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
324 |
< |
t1.join(); |
325 |
< |
t2.join(); |
352 |
< |
} catch(Exception e){ |
353 |
< |
unexpectedException(); |
354 |
< |
} |
355 |
< |
} |
308 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
309 |
> |
sync.acquire(1); |
310 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
311 |
> |
t1.start(); |
312 |
> |
delay(SHORT_DELAY_MS); |
313 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
314 |
> |
t2.start(); |
315 |
> |
delay(SHORT_DELAY_MS); |
316 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
317 |
> |
t1.interrupt(); |
318 |
> |
delay(SHORT_DELAY_MS); |
319 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
320 |
> |
sync.release(1); |
321 |
> |
delay(SHORT_DELAY_MS); |
322 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
323 |
> |
t1.join(); |
324 |
> |
t2.join(); |
325 |
> |
} |
326 |
|
|
327 |
|
/** |
328 |
|
* tryAcquireNanos is interruptible. |
329 |
|
*/ |
330 |
< |
public void testInterruptedException2() { |
331 |
< |
final Mutex sync = new Mutex(); |
332 |
< |
sync.acquire(1); |
333 |
< |
Thread t = new Thread(new Runnable() { |
334 |
< |
public void run() { |
335 |
< |
try { |
336 |
< |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
337 |
< |
threadShouldThrow(); |
338 |
< |
} catch(InterruptedException success){} |
339 |
< |
} |
340 |
< |
}); |
341 |
< |
try { |
372 |
< |
t.start(); |
373 |
< |
t.interrupt(); |
374 |
< |
} catch(Exception e){ |
375 |
< |
unexpectedException(); |
376 |
< |
} |
330 |
> |
public void testInterruptedException2() throws InterruptedException { |
331 |
> |
final Mutex sync = new Mutex(); |
332 |
> |
sync.acquire(1); |
333 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
334 |
> |
public void realRun() throws InterruptedException { |
335 |
> |
sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS)); |
336 |
> |
}}); |
337 |
> |
|
338 |
> |
t.start(); |
339 |
> |
delay(SHORT_DELAY_MS); |
340 |
> |
t.interrupt(); |
341 |
> |
t.join(); |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
|
/** |
346 |
|
* TryAcquire on exclusively held sync fails |
347 |
|
*/ |
348 |
< |
public void testTryAcquireWhenSynced() { |
349 |
< |
final Mutex sync = new Mutex(); |
350 |
< |
sync.acquire(1); |
351 |
< |
Thread t = new Thread(new Runnable() { |
352 |
< |
public void run() { |
353 |
< |
threadAssertFalse(sync.tryAcquire(1)); |
354 |
< |
} |
355 |
< |
}); |
356 |
< |
try { |
357 |
< |
t.start(); |
358 |
< |
t.join(); |
359 |
< |
sync.release(1); |
395 |
< |
} catch(Exception e){ |
396 |
< |
unexpectedException(); |
397 |
< |
} |
398 |
< |
} |
348 |
> |
public void testTryAcquireWhenSynced() throws InterruptedException { |
349 |
> |
final Mutex sync = new Mutex(); |
350 |
> |
sync.acquire(1); |
351 |
> |
Thread t = new Thread(new CheckedRunnable() { |
352 |
> |
public void realRun() { |
353 |
> |
assertFalse(sync.tryAcquire(1)); |
354 |
> |
}}); |
355 |
> |
|
356 |
> |
t.start(); |
357 |
> |
t.join(); |
358 |
> |
sync.release(1); |
359 |
> |
} |
360 |
|
|
361 |
|
/** |
362 |
|
* tryAcquireNanos on an exclusively held sync times out |
363 |
|
*/ |
364 |
< |
public void testAcquireNanos_Timeout() { |
365 |
< |
final Mutex sync = new Mutex(); |
366 |
< |
sync.acquire(1); |
367 |
< |
Thread t = new Thread(new Runnable() { |
368 |
< |
public void run() { |
369 |
< |
try { |
370 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
371 |
< |
} catch (Exception ex) { |
372 |
< |
threadUnexpectedException(); |
373 |
< |
} |
374 |
< |
} |
375 |
< |
}); |
376 |
< |
try { |
377 |
< |
t.start(); |
378 |
< |
t.join(); |
418 |
< |
sync.release(1); |
419 |
< |
} catch(Exception e){ |
420 |
< |
unexpectedException(); |
421 |
< |
} |
422 |
< |
} |
423 |
< |
|
424 |
< |
|
364 |
> |
public void testAcquireNanos_Timeout() throws InterruptedException { |
365 |
> |
final Mutex sync = new Mutex(); |
366 |
> |
sync.acquire(1); |
367 |
> |
Thread t = new Thread(new CheckedRunnable() { |
368 |
> |
public void realRun() throws InterruptedException { |
369 |
> |
long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS); |
370 |
> |
assertFalse(sync.tryAcquireNanos(1, nanos)); |
371 |
> |
}}); |
372 |
> |
|
373 |
> |
t.start(); |
374 |
> |
t.join(); |
375 |
> |
sync.release(1); |
376 |
> |
} |
377 |
> |
|
378 |
> |
|
379 |
|
/** |
380 |
|
* getState is true when acquired and false when not |
381 |
|
*/ |
382 |
< |
public void testGetState() { |
383 |
< |
final Mutex sync = new Mutex(); |
384 |
< |
sync.acquire(1); |
385 |
< |
assertTrue(sync.isHeldExclusively()); |
386 |
< |
sync.release(1); |
387 |
< |
assertFalse(sync.isHeldExclusively()); |
388 |
< |
Thread t = new Thread(new Runnable() { |
389 |
< |
public void run() { |
390 |
< |
sync.acquire(1); |
391 |
< |
try { |
392 |
< |
Thread.sleep(SMALL_DELAY_MS); |
393 |
< |
} |
394 |
< |
catch(Exception e) { |
395 |
< |
threadUnexpectedException(); |
396 |
< |
} |
397 |
< |
sync.release(1); |
398 |
< |
} |
399 |
< |
}); |
446 |
< |
try { |
447 |
< |
t.start(); |
448 |
< |
Thread.sleep(SHORT_DELAY_MS); |
449 |
< |
assertTrue(sync.isHeldExclusively()); |
450 |
< |
t.join(); |
451 |
< |
assertFalse(sync.isHeldExclusively()); |
452 |
< |
} catch(Exception e){ |
453 |
< |
unexpectedException(); |
454 |
< |
} |
382 |
> |
public void testGetState() throws InterruptedException { |
383 |
> |
final Mutex sync = new Mutex(); |
384 |
> |
sync.acquire(1); |
385 |
> |
assertTrue(sync.isHeldExclusively()); |
386 |
> |
sync.release(1); |
387 |
> |
assertFalse(sync.isHeldExclusively()); |
388 |
> |
Thread t = new Thread(new CheckedRunnable() { |
389 |
> |
public void realRun() throws InterruptedException { |
390 |
> |
sync.acquire(1); |
391 |
> |
delay(SMALL_DELAY_MS); |
392 |
> |
sync.release(1); |
393 |
> |
}}); |
394 |
> |
|
395 |
> |
t.start(); |
396 |
> |
delay(SHORT_DELAY_MS); |
397 |
> |
assertTrue(sync.isHeldExclusively()); |
398 |
> |
t.join(); |
399 |
> |
assertFalse(sync.isHeldExclusively()); |
400 |
|
} |
401 |
|
|
402 |
|
|
403 |
|
/** |
404 |
|
* acquireInterruptibly is interruptible. |
405 |
|
*/ |
406 |
< |
public void testAcquireInterruptibly1() { |
407 |
< |
final Mutex sync = new Mutex(); |
408 |
< |
sync.acquire(1); |
409 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
410 |
< |
try { |
411 |
< |
t.start(); |
412 |
< |
t.interrupt(); |
413 |
< |
sync.release(1); |
414 |
< |
t.join(); |
415 |
< |
} catch(Exception e){ |
416 |
< |
unexpectedException(); |
472 |
< |
} |
473 |
< |
} |
406 |
> |
public void testAcquireInterruptibly1() throws InterruptedException { |
407 |
> |
final Mutex sync = new Mutex(); |
408 |
> |
sync.acquire(1); |
409 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
410 |
> |
t.start(); |
411 |
> |
delay(SHORT_DELAY_MS); |
412 |
> |
t.interrupt(); |
413 |
> |
delay(SHORT_DELAY_MS); |
414 |
> |
sync.release(1); |
415 |
> |
t.join(); |
416 |
> |
} |
417 |
|
|
418 |
|
/** |
419 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
420 |
|
*/ |
421 |
< |
public void testAcquireInterruptibly2() { |
422 |
< |
final Mutex sync = new Mutex(); |
423 |
< |
try { |
424 |
< |
sync.acquireInterruptibly(1); |
425 |
< |
} catch(Exception e) { |
426 |
< |
unexpectedException(); |
427 |
< |
} |
428 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
429 |
< |
try { |
487 |
< |
t.start(); |
488 |
< |
t.interrupt(); |
489 |
< |
assertTrue(sync.isHeldExclusively()); |
490 |
< |
t.join(); |
491 |
< |
} catch(Exception e){ |
492 |
< |
unexpectedException(); |
493 |
< |
} |
421 |
> |
public void testAcquireInterruptibly2() throws InterruptedException { |
422 |
> |
final Mutex sync = new Mutex(); |
423 |
> |
sync.acquireInterruptibly(1); |
424 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
425 |
> |
t.start(); |
426 |
> |
delay(SHORT_DELAY_MS); |
427 |
> |
t.interrupt(); |
428 |
> |
assertTrue(sync.isHeldExclusively()); |
429 |
> |
t.join(); |
430 |
|
} |
431 |
|
|
432 |
|
/** |
433 |
|
* owns is true for a condition created by sync else false |
434 |
|
*/ |
435 |
|
public void testOwns() { |
436 |
< |
final Mutex sync = new Mutex(); |
436 |
> |
final Mutex sync = new Mutex(); |
437 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
438 |
|
final Mutex sync2 = new Mutex(); |
439 |
|
assertTrue(sync.owns(c)); |
443 |
|
/** |
444 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
445 |
|
*/ |
446 |
< |
public void testAwait_IllegalMonitor() { |
447 |
< |
final Mutex sync = new Mutex(); |
446 |
> |
public void testAwait_IllegalMonitor() throws InterruptedException { |
447 |
> |
final Mutex sync = new Mutex(); |
448 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
449 |
|
try { |
450 |
|
c.await(); |
451 |
|
shouldThrow(); |
452 |
< |
} |
517 |
< |
catch (IllegalMonitorStateException success) { |
518 |
< |
} |
519 |
< |
catch (Exception ex) { |
520 |
< |
unexpectedException(); |
521 |
< |
} |
452 |
> |
} catch (IllegalMonitorStateException success) {} |
453 |
|
} |
454 |
|
|
455 |
|
/** |
456 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
457 |
|
*/ |
458 |
< |
public void testSignal_IllegalMonitor() { |
459 |
< |
final Mutex sync = new Mutex(); |
458 |
> |
public void testSignal_IllegalMonitor() throws InterruptedException { |
459 |
> |
final Mutex sync = new Mutex(); |
460 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
461 |
|
try { |
462 |
|
c.signal(); |
463 |
|
shouldThrow(); |
464 |
< |
} |
534 |
< |
catch (IllegalMonitorStateException success) { |
535 |
< |
} |
536 |
< |
catch (Exception ex) { |
537 |
< |
unexpectedException(); |
538 |
< |
} |
464 |
> |
} catch (IllegalMonitorStateException success) {} |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
|
* awaitNanos without a signal times out |
469 |
|
*/ |
470 |
< |
public void testAwaitNanos_Timeout() { |
471 |
< |
final Mutex sync = new Mutex(); |
470 |
> |
public void testAwaitNanos_Timeout() throws InterruptedException { |
471 |
> |
final Mutex sync = new Mutex(); |
472 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
473 |
< |
try { |
474 |
< |
sync.acquire(1); |
475 |
< |
long t = c.awaitNanos(100); |
476 |
< |
assertTrue(t <= 0); |
551 |
< |
sync.release(1); |
552 |
< |
} |
553 |
< |
catch (Exception ex) { |
554 |
< |
unexpectedException(); |
555 |
< |
} |
473 |
> |
sync.acquire(1); |
474 |
> |
long t = c.awaitNanos(100); |
475 |
> |
assertTrue(t <= 0); |
476 |
> |
sync.release(1); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
< |
* Timed await without a signal times out |
480 |
> |
* Timed await without a signal times out |
481 |
|
*/ |
482 |
< |
public void testAwait_Timeout() { |
483 |
< |
final Mutex sync = new Mutex(); |
482 |
> |
public void testAwait_Timeout() throws InterruptedException { |
483 |
> |
final Mutex sync = new Mutex(); |
484 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
485 |
< |
try { |
486 |
< |
sync.acquire(1); |
487 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
567 |
< |
sync.release(1); |
568 |
< |
} |
569 |
< |
catch (Exception ex) { |
570 |
< |
unexpectedException(); |
571 |
< |
} |
485 |
> |
sync.acquire(1); |
486 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
487 |
> |
sync.release(1); |
488 |
|
} |
489 |
|
|
490 |
|
/** |
491 |
|
* awaitUntil without a signal times out |
492 |
|
*/ |
493 |
< |
public void testAwaitUntil_Timeout() { |
494 |
< |
final Mutex sync = new Mutex(); |
493 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
494 |
> |
final Mutex sync = new Mutex(); |
495 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
496 |
< |
try { |
497 |
< |
sync.acquire(1); |
498 |
< |
java.util.Date d = new java.util.Date(); |
499 |
< |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
584 |
< |
sync.release(1); |
585 |
< |
} |
586 |
< |
catch (Exception ex) { |
587 |
< |
unexpectedException(); |
588 |
< |
} |
496 |
> |
sync.acquire(1); |
497 |
> |
java.util.Date d = new java.util.Date(); |
498 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
499 |
> |
sync.release(1); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
|
* await returns when signalled |
504 |
|
*/ |
505 |
< |
public void testAwait() { |
506 |
< |
final Mutex sync = new Mutex(); |
505 |
> |
public void testAwait() throws InterruptedException { |
506 |
> |
final Mutex sync = new Mutex(); |
507 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
508 |
< |
Thread t = new Thread(new Runnable() { |
509 |
< |
public void run() { |
510 |
< |
try { |
511 |
< |
sync.acquire(1); |
512 |
< |
c.await(); |
513 |
< |
sync.release(1); |
603 |
< |
} |
604 |
< |
catch(InterruptedException e) { |
605 |
< |
threadUnexpectedException(); |
606 |
< |
} |
607 |
< |
} |
608 |
< |
}); |
508 |
> |
Thread t = new Thread(new CheckedRunnable() { |
509 |
> |
public void realRun() throws InterruptedException { |
510 |
> |
sync.acquire(1); |
511 |
> |
c.await(); |
512 |
> |
sync.release(1); |
513 |
> |
}}); |
514 |
|
|
515 |
< |
try { |
516 |
< |
t.start(); |
517 |
< |
Thread.sleep(SHORT_DELAY_MS); |
518 |
< |
sync.acquire(1); |
519 |
< |
c.signal(); |
520 |
< |
sync.release(1); |
521 |
< |
t.join(SHORT_DELAY_MS); |
617 |
< |
assertFalse(t.isAlive()); |
618 |
< |
} |
619 |
< |
catch (Exception ex) { |
620 |
< |
unexpectedException(); |
621 |
< |
} |
515 |
> |
t.start(); |
516 |
> |
delay(SHORT_DELAY_MS); |
517 |
> |
sync.acquire(1); |
518 |
> |
c.signal(); |
519 |
> |
sync.release(1); |
520 |
> |
t.join(SHORT_DELAY_MS); |
521 |
> |
assertFalse(t.isAlive()); |
522 |
|
} |
523 |
|
|
524 |
|
|
527 |
|
* hasWaiters throws NPE if null |
528 |
|
*/ |
529 |
|
public void testHasWaitersNPE() { |
530 |
< |
final Mutex sync = new Mutex(); |
530 |
> |
final Mutex sync = new Mutex(); |
531 |
|
try { |
532 |
|
sync.hasWaiters(null); |
533 |
|
shouldThrow(); |
534 |
< |
} catch (NullPointerException success) { |
635 |
< |
} catch (Exception ex) { |
636 |
< |
unexpectedException(); |
637 |
< |
} |
534 |
> |
} catch (NullPointerException success) {} |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
|
* getWaitQueueLength throws NPE if null |
539 |
|
*/ |
540 |
|
public void testGetWaitQueueLengthNPE() { |
541 |
< |
final Mutex sync = new Mutex(); |
541 |
> |
final Mutex sync = new Mutex(); |
542 |
|
try { |
543 |
|
sync.getWaitQueueLength(null); |
544 |
|
shouldThrow(); |
545 |
< |
} catch (NullPointerException success) { |
649 |
< |
} catch (Exception ex) { |
650 |
< |
unexpectedException(); |
651 |
< |
} |
545 |
> |
} catch (NullPointerException success) {} |
546 |
|
} |
547 |
|
|
548 |
|
|
550 |
|
* getWaitingThreads throws NPE if null |
551 |
|
*/ |
552 |
|
public void testGetWaitingThreadsNPE() { |
553 |
< |
final Mutex sync = new Mutex(); |
553 |
> |
final Mutex sync = new Mutex(); |
554 |
|
try { |
555 |
|
sync.getWaitingThreads(null); |
556 |
|
shouldThrow(); |
557 |
< |
} catch (NullPointerException success) { |
664 |
< |
} catch (Exception ex) { |
665 |
< |
unexpectedException(); |
666 |
< |
} |
557 |
> |
} catch (NullPointerException success) {} |
558 |
|
} |
559 |
|
|
560 |
|
|
562 |
|
* hasWaiters throws IAE if not owned |
563 |
|
*/ |
564 |
|
public void testHasWaitersIAE() { |
565 |
< |
final Mutex sync = new Mutex(); |
566 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
567 |
< |
final Mutex sync2 = new Mutex(); |
565 |
> |
final Mutex sync = new Mutex(); |
566 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
567 |
> |
final Mutex sync2 = new Mutex(); |
568 |
|
try { |
569 |
|
sync2.hasWaiters(c); |
570 |
|
shouldThrow(); |
571 |
< |
} catch (IllegalArgumentException success) { |
681 |
< |
} catch (Exception ex) { |
682 |
< |
unexpectedException(); |
683 |
< |
} |
571 |
> |
} catch (IllegalArgumentException success) {} |
572 |
|
} |
573 |
|
|
574 |
|
/** |
575 |
|
* hasWaiters throws IMSE if not synced |
576 |
|
*/ |
577 |
|
public void testHasWaitersIMSE() { |
578 |
< |
final Mutex sync = new Mutex(); |
579 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
578 |
> |
final Mutex sync = new Mutex(); |
579 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
580 |
|
try { |
581 |
|
sync.hasWaiters(c); |
582 |
|
shouldThrow(); |
583 |
< |
} catch (IllegalMonitorStateException success) { |
696 |
< |
} catch (Exception ex) { |
697 |
< |
unexpectedException(); |
698 |
< |
} |
583 |
> |
} catch (IllegalMonitorStateException success) {} |
584 |
|
} |
585 |
|
|
586 |
|
|
588 |
|
* getWaitQueueLength throws IAE if not owned |
589 |
|
*/ |
590 |
|
public void testGetWaitQueueLengthIAE() { |
591 |
< |
final Mutex sync = new Mutex(); |
592 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
593 |
< |
final Mutex sync2 = new Mutex(); |
591 |
> |
final Mutex sync = new Mutex(); |
592 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
593 |
> |
final Mutex sync2 = new Mutex(); |
594 |
|
try { |
595 |
|
sync2.getWaitQueueLength(c); |
596 |
|
shouldThrow(); |
597 |
< |
} catch (IllegalArgumentException success) { |
713 |
< |
} catch (Exception ex) { |
714 |
< |
unexpectedException(); |
715 |
< |
} |
597 |
> |
} catch (IllegalArgumentException success) {} |
598 |
|
} |
599 |
|
|
600 |
|
/** |
601 |
|
* getWaitQueueLength throws IMSE if not synced |
602 |
|
*/ |
603 |
|
public void testGetWaitQueueLengthIMSE() { |
604 |
< |
final Mutex sync = new Mutex(); |
605 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
604 |
> |
final Mutex sync = new Mutex(); |
605 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
606 |
|
try { |
607 |
|
sync.getWaitQueueLength(c); |
608 |
|
shouldThrow(); |
609 |
< |
} catch (IllegalMonitorStateException success) { |
728 |
< |
} catch (Exception ex) { |
729 |
< |
unexpectedException(); |
730 |
< |
} |
609 |
> |
} catch (IllegalMonitorStateException success) {} |
610 |
|
} |
611 |
|
|
612 |
|
|
614 |
|
* getWaitingThreads throws IAE if not owned |
615 |
|
*/ |
616 |
|
public void testGetWaitingThreadsIAE() { |
617 |
< |
final Mutex sync = new Mutex(); |
618 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
619 |
< |
final Mutex sync2 = new Mutex(); |
617 |
> |
final Mutex sync = new Mutex(); |
618 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
619 |
> |
final Mutex sync2 = new Mutex(); |
620 |
|
try { |
621 |
|
sync2.getWaitingThreads(c); |
622 |
|
shouldThrow(); |
623 |
< |
} catch (IllegalArgumentException success) { |
745 |
< |
} catch (Exception ex) { |
746 |
< |
unexpectedException(); |
747 |
< |
} |
623 |
> |
} catch (IllegalArgumentException success) {} |
624 |
|
} |
625 |
|
|
626 |
|
/** |
627 |
|
* getWaitingThreads throws IMSE if not synced |
628 |
|
*/ |
629 |
|
public void testGetWaitingThreadsIMSE() { |
630 |
< |
final Mutex sync = new Mutex(); |
631 |
< |
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
630 |
> |
final Mutex sync = new Mutex(); |
631 |
> |
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
632 |
|
try { |
633 |
|
sync.getWaitingThreads(c); |
634 |
|
shouldThrow(); |
635 |
< |
} catch (IllegalMonitorStateException success) { |
760 |
< |
} catch (Exception ex) { |
761 |
< |
unexpectedException(); |
762 |
< |
} |
635 |
> |
} catch (IllegalMonitorStateException success) {} |
636 |
|
} |
637 |
|
|
638 |
|
|
640 |
|
/** |
641 |
|
* hasWaiters returns true when a thread is waiting, else false |
642 |
|
*/ |
643 |
< |
public void testHasWaiters() { |
644 |
< |
final Mutex sync = new Mutex(); |
643 |
> |
public void testHasWaiters() throws InterruptedException { |
644 |
> |
final Mutex sync = new Mutex(); |
645 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
646 |
< |
Thread t = new Thread(new Runnable() { |
647 |
< |
public void run() { |
648 |
< |
try { |
649 |
< |
sync.acquire(1); |
650 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
651 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
652 |
< |
c.await(); |
653 |
< |
sync.release(1); |
781 |
< |
} |
782 |
< |
catch(InterruptedException e) { |
783 |
< |
threadUnexpectedException(); |
784 |
< |
} |
785 |
< |
} |
786 |
< |
}); |
646 |
> |
Thread t = new Thread(new CheckedRunnable() { |
647 |
> |
public void realRun() throws InterruptedException { |
648 |
> |
sync.acquire(1); |
649 |
> |
assertFalse(sync.hasWaiters(c)); |
650 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
651 |
> |
c.await(); |
652 |
> |
sync.release(1); |
653 |
> |
}}); |
654 |
|
|
655 |
< |
try { |
656 |
< |
t.start(); |
657 |
< |
Thread.sleep(SHORT_DELAY_MS); |
658 |
< |
sync.acquire(1); |
659 |
< |
assertTrue(sync.hasWaiters(c)); |
660 |
< |
assertEquals(1, sync.getWaitQueueLength(c)); |
661 |
< |
c.signal(); |
662 |
< |
sync.release(1); |
663 |
< |
Thread.sleep(SHORT_DELAY_MS); |
664 |
< |
sync.acquire(1); |
665 |
< |
assertFalse(sync.hasWaiters(c)); |
666 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
667 |
< |
sync.release(1); |
668 |
< |
t.join(SHORT_DELAY_MS); |
802 |
< |
assertFalse(t.isAlive()); |
803 |
< |
} |
804 |
< |
catch (Exception ex) { |
805 |
< |
unexpectedException(); |
806 |
< |
} |
655 |
> |
t.start(); |
656 |
> |
delay(SHORT_DELAY_MS); |
657 |
> |
sync.acquire(1); |
658 |
> |
assertTrue(sync.hasWaiters(c)); |
659 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
660 |
> |
c.signal(); |
661 |
> |
sync.release(1); |
662 |
> |
delay(SHORT_DELAY_MS); |
663 |
> |
sync.acquire(1); |
664 |
> |
assertFalse(sync.hasWaiters(c)); |
665 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
666 |
> |
sync.release(1); |
667 |
> |
t.join(SHORT_DELAY_MS); |
668 |
> |
assertFalse(t.isAlive()); |
669 |
|
} |
670 |
|
|
671 |
|
/** |
672 |
|
* getWaitQueueLength returns number of waiting threads |
673 |
|
*/ |
674 |
< |
public void testGetWaitQueueLength() { |
675 |
< |
final Mutex sync = new Mutex(); |
674 |
> |
public void testGetWaitQueueLength() throws InterruptedException { |
675 |
> |
final Mutex sync = new Mutex(); |
676 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
677 |
< |
Thread t1 = new Thread(new Runnable() { |
678 |
< |
public void run() { |
679 |
< |
try { |
680 |
< |
sync.acquire(1); |
681 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
682 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
683 |
< |
c.await(); |
684 |
< |
sync.release(1); |
685 |
< |
} |
686 |
< |
catch(InterruptedException e) { |
687 |
< |
threadUnexpectedException(); |
688 |
< |
} |
689 |
< |
} |
690 |
< |
}); |
691 |
< |
|
692 |
< |
Thread t2 = new Thread(new Runnable() { |
693 |
< |
public void run() { |
694 |
< |
try { |
695 |
< |
sync.acquire(1); |
696 |
< |
threadAssertTrue(sync.hasWaiters(c)); |
697 |
< |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
698 |
< |
c.await(); |
699 |
< |
sync.release(1); |
700 |
< |
} |
701 |
< |
catch(InterruptedException e) { |
702 |
< |
threadUnexpectedException(); |
703 |
< |
} |
704 |
< |
} |
705 |
< |
}); |
706 |
< |
|
707 |
< |
try { |
708 |
< |
t1.start(); |
709 |
< |
Thread.sleep(SHORT_DELAY_MS); |
710 |
< |
t2.start(); |
711 |
< |
Thread.sleep(SHORT_DELAY_MS); |
712 |
< |
sync.acquire(1); |
851 |
< |
assertTrue(sync.hasWaiters(c)); |
852 |
< |
assertEquals(2, sync.getWaitQueueLength(c)); |
853 |
< |
c.signalAll(); |
854 |
< |
sync.release(1); |
855 |
< |
Thread.sleep(SHORT_DELAY_MS); |
856 |
< |
sync.acquire(1); |
857 |
< |
assertFalse(sync.hasWaiters(c)); |
858 |
< |
assertEquals(0, sync.getWaitQueueLength(c)); |
859 |
< |
sync.release(1); |
860 |
< |
t1.join(SHORT_DELAY_MS); |
861 |
< |
t2.join(SHORT_DELAY_MS); |
862 |
< |
assertFalse(t1.isAlive()); |
863 |
< |
assertFalse(t2.isAlive()); |
864 |
< |
} |
865 |
< |
catch (Exception ex) { |
866 |
< |
unexpectedException(); |
867 |
< |
} |
677 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
678 |
> |
public void realRun() throws InterruptedException { |
679 |
> |
sync.acquire(1); |
680 |
> |
assertFalse(sync.hasWaiters(c)); |
681 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
682 |
> |
c.await(); |
683 |
> |
sync.release(1); |
684 |
> |
}}); |
685 |
> |
|
686 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
687 |
> |
public void realRun() throws InterruptedException { |
688 |
> |
sync.acquire(1); |
689 |
> |
assertTrue(sync.hasWaiters(c)); |
690 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
691 |
> |
c.await(); |
692 |
> |
sync.release(1); |
693 |
> |
}}); |
694 |
> |
|
695 |
> |
t1.start(); |
696 |
> |
delay(SHORT_DELAY_MS); |
697 |
> |
t2.start(); |
698 |
> |
delay(SHORT_DELAY_MS); |
699 |
> |
sync.acquire(1); |
700 |
> |
assertTrue(sync.hasWaiters(c)); |
701 |
> |
assertEquals(2, sync.getWaitQueueLength(c)); |
702 |
> |
c.signalAll(); |
703 |
> |
sync.release(1); |
704 |
> |
delay(SHORT_DELAY_MS); |
705 |
> |
sync.acquire(1); |
706 |
> |
assertFalse(sync.hasWaiters(c)); |
707 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
708 |
> |
sync.release(1); |
709 |
> |
t1.join(SHORT_DELAY_MS); |
710 |
> |
t2.join(SHORT_DELAY_MS); |
711 |
> |
assertFalse(t1.isAlive()); |
712 |
> |
assertFalse(t2.isAlive()); |
713 |
|
} |
714 |
|
|
715 |
|
/** |
716 |
|
* getWaitingThreads returns only and all waiting threads |
717 |
|
*/ |
718 |
< |
public void testGetWaitingThreads() { |
719 |
< |
final Mutex sync = new Mutex(); |
718 |
> |
public void testGetWaitingThreads() throws InterruptedException { |
719 |
> |
final Mutex sync = new Mutex(); |
720 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
721 |
< |
Thread t1 = new Thread(new Runnable() { |
722 |
< |
public void run() { |
723 |
< |
try { |
724 |
< |
sync.acquire(1); |
725 |
< |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
726 |
< |
c.await(); |
727 |
< |
sync.release(1); |
728 |
< |
} |
729 |
< |
catch(InterruptedException e) { |
730 |
< |
threadUnexpectedException(); |
731 |
< |
} |
732 |
< |
} |
733 |
< |
}); |
734 |
< |
|
735 |
< |
Thread t2 = new Thread(new Runnable() { |
891 |
< |
public void run() { |
892 |
< |
try { |
893 |
< |
sync.acquire(1); |
894 |
< |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
895 |
< |
c.await(); |
896 |
< |
sync.release(1); |
897 |
< |
} |
898 |
< |
catch(InterruptedException e) { |
899 |
< |
threadUnexpectedException(); |
900 |
< |
} |
901 |
< |
} |
902 |
< |
}); |
721 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
722 |
> |
public void realRun() throws InterruptedException { |
723 |
> |
sync.acquire(1); |
724 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
725 |
> |
c.await(); |
726 |
> |
sync.release(1); |
727 |
> |
}}); |
728 |
> |
|
729 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
730 |
> |
public void realRun() throws InterruptedException { |
731 |
> |
sync.acquire(1); |
732 |
> |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
733 |
> |
c.await(); |
734 |
> |
sync.release(1); |
735 |
> |
}}); |
736 |
|
|
904 |
– |
try { |
737 |
|
sync.acquire(1); |
738 |
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
739 |
|
sync.release(1); |
740 |
|
t1.start(); |
741 |
< |
Thread.sleep(SHORT_DELAY_MS); |
741 |
> |
delay(SHORT_DELAY_MS); |
742 |
|
t2.start(); |
743 |
< |
Thread.sleep(SHORT_DELAY_MS); |
743 |
> |
delay(SHORT_DELAY_MS); |
744 |
|
sync.acquire(1); |
745 |
|
assertTrue(sync.hasWaiters(c)); |
746 |
|
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
747 |
|
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
748 |
|
c.signalAll(); |
749 |
|
sync.release(1); |
750 |
< |
Thread.sleep(SHORT_DELAY_MS); |
750 |
> |
delay(SHORT_DELAY_MS); |
751 |
|
sync.acquire(1); |
752 |
|
assertFalse(sync.hasWaiters(c)); |
753 |
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
756 |
|
t2.join(SHORT_DELAY_MS); |
757 |
|
assertFalse(t1.isAlive()); |
758 |
|
assertFalse(t2.isAlive()); |
927 |
– |
} |
928 |
– |
catch (Exception ex) { |
929 |
– |
unexpectedException(); |
930 |
– |
} |
759 |
|
} |
760 |
|
|
761 |
|
|
763 |
|
/** |
764 |
|
* awaitUninterruptibly doesn't abort on interrupt |
765 |
|
*/ |
766 |
< |
public void testAwaitUninterruptibly() { |
767 |
< |
final Mutex sync = new Mutex(); |
766 |
> |
public void testAwaitUninterruptibly() throws InterruptedException { |
767 |
> |
final Mutex sync = new Mutex(); |
768 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
769 |
< |
Thread t = new Thread(new Runnable() { |
770 |
< |
public void run() { |
771 |
< |
sync.acquire(1); |
772 |
< |
c.awaitUninterruptibly(); |
773 |
< |
sync.release(1); |
774 |
< |
} |
947 |
< |
}); |
769 |
> |
Thread t = new Thread(new CheckedRunnable() { |
770 |
> |
public void realRun() { |
771 |
> |
sync.acquire(1); |
772 |
> |
c.awaitUninterruptibly(); |
773 |
> |
sync.release(1); |
774 |
> |
}}); |
775 |
|
|
776 |
< |
try { |
777 |
< |
t.start(); |
778 |
< |
Thread.sleep(SHORT_DELAY_MS); |
779 |
< |
t.interrupt(); |
780 |
< |
sync.acquire(1); |
781 |
< |
c.signal(); |
782 |
< |
sync.release(1); |
783 |
< |
assert(t.isInterrupted()); |
957 |
< |
t.join(SHORT_DELAY_MS); |
958 |
< |
assertFalse(t.isAlive()); |
959 |
< |
} |
960 |
< |
catch (Exception ex) { |
961 |
< |
unexpectedException(); |
962 |
< |
} |
776 |
> |
t.start(); |
777 |
> |
delay(SHORT_DELAY_MS); |
778 |
> |
t.interrupt(); |
779 |
> |
sync.acquire(1); |
780 |
> |
c.signal(); |
781 |
> |
sync.release(1); |
782 |
> |
t.join(SHORT_DELAY_MS); |
783 |
> |
assertFalse(t.isAlive()); |
784 |
|
} |
785 |
|
|
786 |
|
/** |
787 |
|
* await is interruptible |
788 |
|
*/ |
789 |
< |
public void testAwait_Interrupt() { |
790 |
< |
final Mutex sync = new Mutex(); |
789 |
> |
public void testAwait_Interrupt() throws InterruptedException { |
790 |
> |
final Mutex sync = new Mutex(); |
791 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
792 |
< |
Thread t = new Thread(new Runnable() { |
793 |
< |
public void run() { |
794 |
< |
try { |
795 |
< |
sync.acquire(1); |
796 |
< |
c.await(); |
976 |
< |
sync.release(1); |
977 |
< |
threadShouldThrow(); |
978 |
< |
} |
979 |
< |
catch(InterruptedException success) { |
980 |
< |
} |
981 |
< |
} |
982 |
< |
}); |
792 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
> |
sync.acquire(1); |
795 |
> |
c.await(); |
796 |
> |
}}); |
797 |
|
|
798 |
< |
try { |
799 |
< |
t.start(); |
800 |
< |
Thread.sleep(SHORT_DELAY_MS); |
801 |
< |
t.interrupt(); |
802 |
< |
t.join(SHORT_DELAY_MS); |
989 |
< |
assertFalse(t.isAlive()); |
990 |
< |
} |
991 |
< |
catch (Exception ex) { |
992 |
< |
unexpectedException(); |
993 |
< |
} |
798 |
> |
t.start(); |
799 |
> |
delay(SHORT_DELAY_MS); |
800 |
> |
t.interrupt(); |
801 |
> |
t.join(SHORT_DELAY_MS); |
802 |
> |
assertFalse(t.isAlive()); |
803 |
|
} |
804 |
|
|
805 |
|
/** |
806 |
|
* awaitNanos is interruptible |
807 |
|
*/ |
808 |
< |
public void testAwaitNanos_Interrupt() { |
809 |
< |
final Mutex sync = new Mutex(); |
808 |
> |
public void testAwaitNanos_Interrupt() throws InterruptedException { |
809 |
> |
final Mutex sync = new Mutex(); |
810 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
811 |
< |
Thread t = new Thread(new Runnable() { |
812 |
< |
public void run() { |
813 |
< |
try { |
814 |
< |
sync.acquire(1); |
815 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
1007 |
< |
sync.release(1); |
1008 |
< |
threadShouldThrow(); |
1009 |
< |
} |
1010 |
< |
catch(InterruptedException success) { |
1011 |
< |
} |
1012 |
< |
} |
1013 |
< |
}); |
811 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
812 |
> |
public void realRun() throws InterruptedException { |
813 |
> |
sync.acquire(1); |
814 |
> |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
815 |
> |
}}); |
816 |
|
|
817 |
< |
try { |
818 |
< |
t.start(); |
819 |
< |
Thread.sleep(SHORT_DELAY_MS); |
820 |
< |
t.interrupt(); |
821 |
< |
t.join(SHORT_DELAY_MS); |
1020 |
< |
assertFalse(t.isAlive()); |
1021 |
< |
} |
1022 |
< |
catch (Exception ex) { |
1023 |
< |
unexpectedException(); |
1024 |
< |
} |
817 |
> |
t.start(); |
818 |
> |
delay(SHORT_DELAY_MS); |
819 |
> |
t.interrupt(); |
820 |
> |
t.join(SHORT_DELAY_MS); |
821 |
> |
assertFalse(t.isAlive()); |
822 |
|
} |
823 |
|
|
824 |
|
/** |
825 |
|
* awaitUntil is interruptible |
826 |
|
*/ |
827 |
< |
public void testAwaitUntil_Interrupt() { |
828 |
< |
final Mutex sync = new Mutex(); |
827 |
> |
public void testAwaitUntil_Interrupt() throws InterruptedException { |
828 |
> |
final Mutex sync = new Mutex(); |
829 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
830 |
< |
Thread t = new Thread(new Runnable() { |
831 |
< |
public void run() { |
832 |
< |
try { |
833 |
< |
sync.acquire(1); |
834 |
< |
java.util.Date d = new java.util.Date(); |
835 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
1039 |
< |
sync.release(1); |
1040 |
< |
threadShouldThrow(); |
1041 |
< |
} |
1042 |
< |
catch(InterruptedException success) { |
1043 |
< |
} |
1044 |
< |
} |
1045 |
< |
}); |
830 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
831 |
> |
public void realRun() throws InterruptedException { |
832 |
> |
sync.acquire(1); |
833 |
> |
java.util.Date d = new java.util.Date(); |
834 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
835 |
> |
}}); |
836 |
|
|
837 |
< |
try { |
838 |
< |
t.start(); |
839 |
< |
Thread.sleep(SHORT_DELAY_MS); |
840 |
< |
t.interrupt(); |
841 |
< |
t.join(SHORT_DELAY_MS); |
1052 |
< |
assertFalse(t.isAlive()); |
1053 |
< |
} |
1054 |
< |
catch (Exception ex) { |
1055 |
< |
unexpectedException(); |
1056 |
< |
} |
837 |
> |
t.start(); |
838 |
> |
delay(SHORT_DELAY_MS); |
839 |
> |
t.interrupt(); |
840 |
> |
t.join(SHORT_DELAY_MS); |
841 |
> |
assertFalse(t.isAlive()); |
842 |
|
} |
843 |
|
|
844 |
|
/** |
845 |
|
* signalAll wakes up all threads |
846 |
|
*/ |
847 |
< |
public void testSignalAll() { |
848 |
< |
final Mutex sync = new Mutex(); |
847 |
> |
public void testSignalAll() throws InterruptedException { |
848 |
> |
final Mutex sync = new Mutex(); |
849 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
850 |
< |
Thread t1 = new Thread(new Runnable() { |
851 |
< |
public void run() { |
852 |
< |
try { |
853 |
< |
sync.acquire(1); |
854 |
< |
c.await(); |
855 |
< |
sync.release(1); |
856 |
< |
} |
857 |
< |
catch(InterruptedException e) { |
858 |
< |
threadUnexpectedException(); |
859 |
< |
} |
860 |
< |
} |
861 |
< |
}); |
862 |
< |
|
863 |
< |
Thread t2 = new Thread(new Runnable() { |
864 |
< |
public void run() { |
865 |
< |
try { |
866 |
< |
sync.acquire(1); |
867 |
< |
c.await(); |
868 |
< |
sync.release(1); |
869 |
< |
} |
870 |
< |
catch(InterruptedException e) { |
871 |
< |
threadUnexpectedException(); |
872 |
< |
} |
873 |
< |
} |
1089 |
< |
}); |
1090 |
< |
|
1091 |
< |
try { |
1092 |
< |
t1.start(); |
1093 |
< |
t2.start(); |
1094 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1095 |
< |
sync.acquire(1); |
1096 |
< |
c.signalAll(); |
1097 |
< |
sync.release(1); |
1098 |
< |
t1.join(SHORT_DELAY_MS); |
1099 |
< |
t2.join(SHORT_DELAY_MS); |
1100 |
< |
assertFalse(t1.isAlive()); |
1101 |
< |
assertFalse(t2.isAlive()); |
1102 |
< |
} |
1103 |
< |
catch (Exception ex) { |
1104 |
< |
unexpectedException(); |
1105 |
< |
} |
850 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
851 |
> |
public void realRun() throws InterruptedException { |
852 |
> |
sync.acquire(1); |
853 |
> |
c.await(); |
854 |
> |
sync.release(1); |
855 |
> |
}}); |
856 |
> |
|
857 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
858 |
> |
public void realRun() throws InterruptedException { |
859 |
> |
sync.acquire(1); |
860 |
> |
c.await(); |
861 |
> |
sync.release(1); |
862 |
> |
}}); |
863 |
> |
|
864 |
> |
t1.start(); |
865 |
> |
t2.start(); |
866 |
> |
delay(SHORT_DELAY_MS); |
867 |
> |
sync.acquire(1); |
868 |
> |
c.signalAll(); |
869 |
> |
sync.release(1); |
870 |
> |
t1.join(SHORT_DELAY_MS); |
871 |
> |
t2.join(SHORT_DELAY_MS); |
872 |
> |
assertFalse(t1.isAlive()); |
873 |
> |
assertFalse(t2.isAlive()); |
874 |
|
} |
875 |
|
|
876 |
|
|
889 |
|
/** |
890 |
|
* A serialized AQS deserializes with current state |
891 |
|
*/ |
892 |
< |
public void testSerialization() { |
892 |
> |
public void testSerialization() throws Exception { |
893 |
|
Mutex l = new Mutex(); |
894 |
|
l.acquire(1); |
895 |
|
assertTrue(l.isHeldExclusively()); |
896 |
|
|
897 |
< |
try { |
898 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
899 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
900 |
< |
out.writeObject(l); |
901 |
< |
out.close(); |
902 |
< |
|
903 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
904 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
905 |
< |
Mutex r = (Mutex) in.readObject(); |
1138 |
< |
assertTrue(r.isHeldExclusively()); |
1139 |
< |
} catch(Exception e){ |
1140 |
< |
e.printStackTrace(); |
1141 |
< |
unexpectedException(); |
1142 |
< |
} |
897 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
898 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
899 |
> |
out.writeObject(l); |
900 |
> |
out.close(); |
901 |
> |
|
902 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
903 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
904 |
> |
Mutex r = (Mutex) in.readObject(); |
905 |
> |
assertTrue(r.isHeldExclusively()); |
906 |
|
} |
907 |
|
|
908 |
|
|
910 |
|
* tryReleaseShared setting state changes getState |
911 |
|
*/ |
912 |
|
public void testGetStateWithReleaseShared() { |
913 |
< |
final BooleanLatch l = new BooleanLatch(); |
914 |
< |
assertFalse(l.isSignalled()); |
915 |
< |
l.releaseShared(0); |
916 |
< |
assertTrue(l.isSignalled()); |
913 |
> |
final BooleanLatch l = new BooleanLatch(); |
914 |
> |
assertFalse(l.isSignalled()); |
915 |
> |
l.releaseShared(0); |
916 |
> |
assertTrue(l.isSignalled()); |
917 |
|
} |
918 |
|
|
919 |
|
/** |
920 |
|
* releaseShared has no effect when already signalled |
921 |
|
*/ |
922 |
|
public void testReleaseShared() { |
923 |
< |
final BooleanLatch l = new BooleanLatch(); |
924 |
< |
assertFalse(l.isSignalled()); |
925 |
< |
l.releaseShared(0); |
926 |
< |
assertTrue(l.isSignalled()); |
927 |
< |
l.releaseShared(0); |
928 |
< |
assertTrue(l.isSignalled()); |
923 |
> |
final BooleanLatch l = new BooleanLatch(); |
924 |
> |
assertFalse(l.isSignalled()); |
925 |
> |
l.releaseShared(0); |
926 |
> |
assertTrue(l.isSignalled()); |
927 |
> |
l.releaseShared(0); |
928 |
> |
assertTrue(l.isSignalled()); |
929 |
|
} |
930 |
|
|
931 |
|
/** |
932 |
|
* acquireSharedInterruptibly returns after release, but not before |
933 |
|
*/ |
934 |
< |
public void testAcquireSharedInterruptibly() { |
935 |
< |
final BooleanLatch l = new BooleanLatch(); |
934 |
> |
public void testAcquireSharedInterruptibly() throws InterruptedException { |
935 |
> |
final BooleanLatch l = new BooleanLatch(); |
936 |
|
|
937 |
< |
Thread t = new Thread(new Runnable() { |
938 |
< |
public void run() { |
939 |
< |
try { |
940 |
< |
threadAssertFalse(l.isSignalled()); |
941 |
< |
l.acquireSharedInterruptibly(0); |
942 |
< |
threadAssertTrue(l.isSignalled()); |
943 |
< |
} catch(InterruptedException e){ |
944 |
< |
threadUnexpectedException(); |
945 |
< |
} |
946 |
< |
} |
947 |
< |
}); |
948 |
< |
try { |
949 |
< |
t.start(); |
1187 |
< |
assertFalse(l.isSignalled()); |
1188 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1189 |
< |
l.releaseShared(0); |
1190 |
< |
assertTrue(l.isSignalled()); |
1191 |
< |
t.join(); |
1192 |
< |
} catch (InterruptedException e){ |
1193 |
< |
unexpectedException(); |
1194 |
< |
} |
937 |
> |
Thread t = new Thread(new CheckedRunnable() { |
938 |
> |
public void realRun() throws InterruptedException { |
939 |
> |
assertFalse(l.isSignalled()); |
940 |
> |
l.acquireSharedInterruptibly(0); |
941 |
> |
assertTrue(l.isSignalled()); |
942 |
> |
}}); |
943 |
> |
|
944 |
> |
t.start(); |
945 |
> |
assertFalse(l.isSignalled()); |
946 |
> |
delay(SHORT_DELAY_MS); |
947 |
> |
l.releaseShared(0); |
948 |
> |
assertTrue(l.isSignalled()); |
949 |
> |
t.join(); |
950 |
|
} |
951 |
< |
|
951 |
> |
|
952 |
|
|
953 |
|
/** |
954 |
|
* acquireSharedTimed returns after release |
955 |
|
*/ |
956 |
< |
public void testAsquireSharedTimed() { |
957 |
< |
final BooleanLatch l = new BooleanLatch(); |
956 |
> |
public void testAcquireSharedTimed() throws InterruptedException { |
957 |
> |
final BooleanLatch l = new BooleanLatch(); |
958 |
|
|
959 |
< |
Thread t = new Thread(new Runnable() { |
960 |
< |
public void run() { |
961 |
< |
try { |
962 |
< |
threadAssertFalse(l.isSignalled()); |
963 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
964 |
< |
threadAssertTrue(l.isSignalled()); |
965 |
< |
|
966 |
< |
} catch(InterruptedException e){ |
967 |
< |
threadUnexpectedException(); |
968 |
< |
} |
969 |
< |
} |
970 |
< |
}); |
971 |
< |
try { |
972 |
< |
t.start(); |
1218 |
< |
assertFalse(l.isSignalled()); |
1219 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1220 |
< |
l.releaseShared(0); |
1221 |
< |
assertTrue(l.isSignalled()); |
1222 |
< |
t.join(); |
1223 |
< |
} catch (InterruptedException e){ |
1224 |
< |
unexpectedException(); |
1225 |
< |
} |
959 |
> |
Thread t = new Thread(new CheckedRunnable() { |
960 |
> |
public void realRun() throws InterruptedException { |
961 |
> |
assertFalse(l.isSignalled()); |
962 |
> |
long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS); |
963 |
> |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
964 |
> |
assertTrue(l.isSignalled()); |
965 |
> |
}}); |
966 |
> |
|
967 |
> |
t.start(); |
968 |
> |
assertFalse(l.isSignalled()); |
969 |
> |
delay(SHORT_DELAY_MS); |
970 |
> |
l.releaseShared(0); |
971 |
> |
assertTrue(l.isSignalled()); |
972 |
> |
t.join(); |
973 |
|
} |
974 |
< |
|
974 |
> |
|
975 |
|
/** |
976 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
977 |
|
*/ |
978 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() { |
978 |
> |
public void testAcquireSharedInterruptibly_InterruptedException() |
979 |
> |
throws InterruptedException { |
980 |
|
final BooleanLatch l = new BooleanLatch(); |
981 |
< |
Thread t = new Thread(new Runnable() { |
982 |
< |
public void run() { |
983 |
< |
try { |
984 |
< |
threadAssertFalse(l.isSignalled()); |
985 |
< |
l.acquireSharedInterruptibly(0); |
986 |
< |
threadShouldThrow(); |
987 |
< |
} catch(InterruptedException success){} |
988 |
< |
} |
989 |
< |
}); |
990 |
< |
t.start(); |
1243 |
< |
try { |
1244 |
< |
assertFalse(l.isSignalled()); |
1245 |
< |
t.interrupt(); |
1246 |
< |
t.join(); |
1247 |
< |
} catch (InterruptedException e){ |
1248 |
< |
unexpectedException(); |
1249 |
< |
} |
981 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
982 |
> |
public void realRun() throws InterruptedException { |
983 |
> |
assertFalse(l.isSignalled()); |
984 |
> |
l.acquireSharedInterruptibly(0); |
985 |
> |
}}); |
986 |
> |
|
987 |
> |
t.start(); |
988 |
> |
assertFalse(l.isSignalled()); |
989 |
> |
t.interrupt(); |
990 |
> |
t.join(); |
991 |
|
} |
992 |
|
|
993 |
|
/** |
994 |
|
* acquireSharedTimed throws IE if interrupted before released |
995 |
|
*/ |
996 |
< |
public void testAcquireSharedNanos_InterruptedException() { |
996 |
> |
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
997 |
|
final BooleanLatch l = new BooleanLatch(); |
998 |
< |
Thread t = new Thread(new Runnable() { |
999 |
< |
public void run() { |
1000 |
< |
try { |
1001 |
< |
threadAssertFalse(l.isSignalled()); |
1002 |
< |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1003 |
< |
threadShouldThrow(); |
1004 |
< |
} catch(InterruptedException success){} |
1264 |
< |
} |
1265 |
< |
}); |
998 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
999 |
> |
public void realRun() throws InterruptedException { |
1000 |
> |
assertFalse(l.isSignalled()); |
1001 |
> |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1002 |
> |
l.tryAcquireSharedNanos(0, nanos); |
1003 |
> |
}}); |
1004 |
> |
|
1005 |
|
t.start(); |
1006 |
< |
try { |
1007 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1008 |
< |
assertFalse(l.isSignalled()); |
1009 |
< |
t.interrupt(); |
1271 |
< |
t.join(); |
1272 |
< |
} catch (InterruptedException e){ |
1273 |
< |
unexpectedException(); |
1274 |
< |
} |
1006 |
> |
delay(SHORT_DELAY_MS); |
1007 |
> |
assertFalse(l.isSignalled()); |
1008 |
> |
t.interrupt(); |
1009 |
> |
t.join(); |
1010 |
|
} |
1011 |
|
|
1012 |
|
/** |
1013 |
|
* acquireSharedTimed times out if not released before timeout |
1014 |
|
*/ |
1015 |
< |
public void testAcquireSharedNanos_Timeout() { |
1015 |
> |
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1016 |
|
final BooleanLatch l = new BooleanLatch(); |
1017 |
< |
Thread t = new Thread(new Runnable() { |
1018 |
< |
public void run() { |
1019 |
< |
try { |
1020 |
< |
threadAssertFalse(l.isSignalled()); |
1021 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1022 |
< |
} catch(InterruptedException ie){ |
1023 |
< |
threadUnexpectedException(); |
1289 |
< |
} |
1290 |
< |
} |
1291 |
< |
}); |
1017 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1018 |
> |
public void realRun() throws InterruptedException { |
1019 |
> |
assertFalse(l.isSignalled()); |
1020 |
> |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1021 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1022 |
> |
}}); |
1023 |
> |
|
1024 |
|
t.start(); |
1025 |
< |
try { |
1026 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1027 |
< |
assertFalse(l.isSignalled()); |
1296 |
< |
t.join(); |
1297 |
< |
} catch (InterruptedException e){ |
1298 |
< |
unexpectedException(); |
1299 |
< |
} |
1025 |
> |
delay(SHORT_DELAY_MS); |
1026 |
> |
assertFalse(l.isSignalled()); |
1027 |
> |
t.join(); |
1028 |
|
} |
1029 |
|
|
1302 |
– |
|
1030 |
|
} |