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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
48 |
|
TimeUnit.SECONDS.convert(1000000000L*t, |
49 |
|
TimeUnit.NANOSECONDS)); |
50 |
|
|
52 |
– |
|
51 |
|
assertEquals(1000L*t*60*60*24, |
52 |
|
TimeUnit.MILLISECONDS.convert(t, |
53 |
|
TimeUnit.DAYS)); |
277 |
|
} |
278 |
|
} |
279 |
|
|
282 |
– |
|
280 |
|
/** |
281 |
|
* convert saturates positive too-large values to Long.MAX_VALUE |
282 |
|
* and negative to LONG.MIN_VALUE |
319 |
|
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
320 |
|
} |
321 |
|
|
325 |
– |
|
322 |
|
/** |
323 |
< |
* toString returns string containing common name of unit |
323 |
> |
* toString returns name of unit |
324 |
|
*/ |
325 |
|
public void testToString() { |
326 |
< |
String s = TimeUnit.SECONDS.toString(); |
331 |
< |
assertTrue(s.indexOf("ECOND") >= 0); |
326 |
> |
assertEquals("SECONDS", TimeUnit.SECONDS.toString()); |
327 |
|
} |
328 |
|
|
329 |
+ |
/** |
330 |
+ |
* name returns name of unit |
331 |
+ |
*/ |
332 |
+ |
public void testName() { |
333 |
+ |
assertEquals("SECONDS", TimeUnit.SECONDS.name()); |
334 |
+ |
} |
335 |
|
|
336 |
|
/** |
337 |
< |
* Timed wait without holding lock throws |
338 |
< |
* IllegalMonitorStateException |
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(); |
350 |
< |
Thread.sleep(SHORT_DELAY_MS); |
351 |
< |
t.interrupt(); |
352 |
< |
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(); |
362 |
– |
|
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 |
|
|
374 |
– |
|
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); |
403 |
< |
}});; |
404 |
< |
s.start(); |
405 |
< |
t.start(); |
406 |
< |
Thread.sleep(SHORT_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 |
> |
|
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 |
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 |
|
} |