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