337 |
|
* Timed wait without holding lock throws |
338 |
|
* IllegalMonitorStateException |
339 |
|
*/ |
340 |
< |
public void testTimedWait_IllegalMonitorException() throws Exception { |
341 |
< |
Thread t = new Thread(new CheckedRunnable() { |
340 |
> |
public void testTimedWait_IllegalMonitorException() { |
341 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
342 |
|
public void realRun() throws InterruptedException { |
343 |
|
Object o = new Object(); |
344 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
345 |
+ |
|
346 |
|
try { |
347 |
< |
tu.timedWait(o,LONG_DELAY_MS); |
347 |
> |
tu.timedWait(o, LONG_DELAY_MS); |
348 |
|
threadShouldThrow(); |
349 |
< |
} catch (IllegalMonitorStateException success) {}}}); |
349 |
> |
} catch (IllegalMonitorStateException success) {} |
350 |
> |
}}); |
351 |
|
|
352 |
< |
t.start(); |
351 |
< |
Thread.sleep(SHORT_DELAY_MS); |
352 |
< |
t.interrupt(); |
353 |
< |
t.join(); |
352 |
> |
awaitTermination(t); |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
|
* timedWait throws InterruptedException when interrupted |
357 |
|
*/ |
358 |
< |
public void testTimedWait() throws InterruptedException { |
359 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
358 |
> |
public void testTimedWait_Interruptible() { |
359 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
360 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
361 |
|
public void realRun() throws InterruptedException { |
362 |
|
Object o = new Object(); |
363 |
– |
|
363 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
364 |
< |
synchronized (o) { |
365 |
< |
tu.timedWait(o,MEDIUM_DELAY_MS); |
366 |
< |
} |
364 |
> |
|
365 |
> |
Thread.currentThread().interrupt(); |
366 |
> |
try { |
367 |
> |
synchronized (o) { |
368 |
> |
tu.timedWait(o, LONG_DELAY_MS); |
369 |
> |
} |
370 |
> |
shouldThrow(); |
371 |
> |
} catch (InterruptedException success) {} |
372 |
> |
assertFalse(Thread.interrupted()); |
373 |
> |
|
374 |
> |
pleaseInterrupt.countDown(); |
375 |
> |
try { |
376 |
> |
synchronized (o) { |
377 |
> |
tu.timedWait(o, LONG_DELAY_MS); |
378 |
> |
} |
379 |
> |
shouldThrow(); |
380 |
> |
} catch (InterruptedException success) {} |
381 |
> |
assertFalse(Thread.interrupted()); |
382 |
|
}}); |
383 |
< |
t.start(); |
384 |
< |
Thread.sleep(SHORT_DELAY_MS); |
383 |
> |
|
384 |
> |
await(pleaseInterrupt); |
385 |
> |
assertThreadStaysAlive(t); |
386 |
|
t.interrupt(); |
387 |
< |
t.join(); |
387 |
> |
awaitTermination(t); |
388 |
|
} |
389 |
|
|
390 |
|
/** |
391 |
|
* timedJoin throws InterruptedException when interrupted |
392 |
|
*/ |
393 |
< |
public void testTimedJoin() throws InterruptedException { |
394 |
< |
final Thread s = new Thread(new CheckedInterruptedRunnable() { |
393 |
> |
public void testTimedJoin_Interruptible() { |
394 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
395 |
> |
final Thread s = newStartedThread(new CheckedInterruptedRunnable() { |
396 |
|
public void realRun() throws InterruptedException { |
397 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
397 |
> |
Thread.sleep(LONG_DELAY_MS); |
398 |
|
}}); |
399 |
< |
final Thread t = new Thread(new CheckedInterruptedRunnable() { |
399 |
> |
final Thread t = newStartedThread(new CheckedRunnable() { |
400 |
|
public void realRun() throws InterruptedException { |
401 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
402 |
< |
tu.timedJoin(s, MEDIUM_DELAY_MS); |
402 |
> |
Thread.currentThread().interrupt(); |
403 |
> |
try { |
404 |
> |
tu.timedJoin(s, LONG_DELAY_MS); |
405 |
> |
shouldThrow(); |
406 |
> |
} catch (InterruptedException success) {} |
407 |
> |
assertFalse(Thread.interrupted()); |
408 |
> |
|
409 |
> |
pleaseInterrupt.countDown(); |
410 |
> |
try { |
411 |
> |
tu.timedJoin(s, LONG_DELAY_MS); |
412 |
> |
shouldThrow(); |
413 |
> |
} catch (InterruptedException success) {} |
414 |
> |
assertFalse(Thread.interrupted()); |
415 |
|
}}); |
416 |
< |
s.start(); |
417 |
< |
t.start(); |
418 |
< |
Thread.sleep(SHORT_DELAY_MS); |
416 |
> |
|
417 |
> |
await(pleaseInterrupt); |
418 |
> |
assertThreadStaysAlive(t); |
419 |
|
t.interrupt(); |
420 |
< |
t.join(); |
420 |
> |
awaitTermination(t); |
421 |
|
s.interrupt(); |
422 |
< |
s.join(); |
422 |
> |
awaitTermination(s); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
|
* timedSleep throws InterruptedException when interrupted |
427 |
|
*/ |
428 |
< |
public void testTimedSleep() throws InterruptedException { |
429 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
428 |
> |
public void testTimedSleep_Interruptible() { |
429 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
430 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
431 |
|
public void realRun() throws InterruptedException { |
432 |
|
TimeUnit tu = TimeUnit.MILLISECONDS; |
433 |
< |
tu.sleep(MEDIUM_DELAY_MS); |
433 |
> |
Thread.currentThread().interrupt(); |
434 |
> |
try { |
435 |
> |
tu.sleep(LONG_DELAY_MS); |
436 |
> |
shouldThrow(); |
437 |
> |
} catch (InterruptedException success) {} |
438 |
> |
assertFalse(Thread.interrupted()); |
439 |
> |
|
440 |
> |
pleaseInterrupt.countDown(); |
441 |
> |
try { |
442 |
> |
tu.sleep(LONG_DELAY_MS); |
443 |
> |
shouldThrow(); |
444 |
> |
} catch (InterruptedException success) {} |
445 |
> |
assertFalse(Thread.interrupted()); |
446 |
|
}}); |
447 |
|
|
448 |
< |
t.start(); |
449 |
< |
Thread.sleep(SHORT_DELAY_MS); |
448 |
> |
await(pleaseInterrupt); |
449 |
> |
assertThreadStaysAlive(t); |
450 |
|
t.interrupt(); |
451 |
< |
t.join(); |
451 |
> |
awaitTermination(t); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
456 |
|
*/ |
457 |
|
public void testSerialization() throws Exception { |
458 |
|
TimeUnit q = TimeUnit.MILLISECONDS; |
459 |
< |
|
419 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
420 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
421 |
< |
out.writeObject(q); |
422 |
< |
out.close(); |
423 |
< |
|
424 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
425 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
426 |
< |
TimeUnit r = (TimeUnit)in.readObject(); |
427 |
< |
assertSame(q, r); |
459 |
> |
assertSame(q, serialClone(q)); |
460 |
|
} |
461 |
|
|
462 |
|
} |