11 |
|
import java.util.concurrent.*; |
12 |
|
import java.util.concurrent.atomic.AtomicReference; |
13 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
|
import java.security.CodeSource; |
16 |
|
import java.security.Permission; |
17 |
|
import java.security.PermissionCollection; |
132 |
|
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
133 |
|
} |
134 |
|
} |
135 |
< |
|
135 |
> |
|
136 |
|
/** |
137 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
138 |
|
*/ |
606 |
|
} |
607 |
|
|
608 |
|
/** |
609 |
+ |
* Waits up to the specified number of milliseconds for the given |
610 |
+ |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
611 |
+ |
*/ |
612 |
+ |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
613 |
+ |
long timeoutNanos = timeoutMillis * 1000L * 1000L; |
614 |
+ |
long t0 = System.nanoTime(); |
615 |
+ |
for (;;) { |
616 |
+ |
Thread.State s = thread.getState(); |
617 |
+ |
if (s == Thread.State.BLOCKED || |
618 |
+ |
s == Thread.State.WAITING || |
619 |
+ |
s == Thread.State.TIMED_WAITING) |
620 |
+ |
return; |
621 |
+ |
else if (s == Thread.State.TERMINATED) |
622 |
+ |
fail("Unexpected thread termination"); |
623 |
+ |
else if (System.nanoTime() - t0 > timeoutNanos) { |
624 |
+ |
threadAssertTrue(thread.isAlive()); |
625 |
+ |
return; |
626 |
+ |
} |
627 |
+ |
Thread.yield(); |
628 |
+ |
} |
629 |
+ |
} |
630 |
+ |
|
631 |
+ |
/** |
632 |
+ |
* Returns the number of milliseconds since time given by |
633 |
+ |
* startNanoTime, which must have been previously returned from a |
634 |
+ |
* call to {@link System.nanoTime()}. |
635 |
+ |
*/ |
636 |
+ |
long millisElapsedSince(long startNanoTime) { |
637 |
+ |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
638 |
+ |
} |
639 |
+ |
|
640 |
+ |
/** |
641 |
|
* Returns a new started daemon Thread running the given runnable. |
642 |
|
*/ |
643 |
|
Thread newStartedThread(Runnable runnable) { |
779 |
|
|
780 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
781 |
|
return new CheckedCallable<String>() { |
782 |
< |
public String realCall() { |
782 |
> |
protected String realCall() { |
783 |
|
try { |
784 |
|
latch.await(); |
785 |
|
} catch (InterruptedException quittingTime) {} |
840 |
|
} |
841 |
|
} |
842 |
|
|
843 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
844 |
+ |
return new CheckedRunnable() { |
845 |
+ |
protected void realRun() { |
846 |
+ |
try { |
847 |
+ |
Thread.sleep(timeoutMillis); |
848 |
+ |
} catch (InterruptedException ok) {} |
849 |
+ |
}}; |
850 |
+ |
} |
851 |
+ |
|
852 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
853 |
|
protected void realRun() { |
854 |
|
try { |