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; |
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 |
+ |
System.nanoTime() - t0 > timeoutNanos) |
621 |
+ |
return; |
622 |
+ |
Thread.yield(); |
623 |
+ |
} |
624 |
+ |
} |
625 |
+ |
|
626 |
+ |
/** |
627 |
+ |
* Returns the number of milliseconds since time given by |
628 |
+ |
* startNanoTime, which must have been previously returned from a |
629 |
+ |
* call to {@link System.nanoTime()}. |
630 |
+ |
*/ |
631 |
+ |
long millisElapsedSince(long startNanoTime) { |
632 |
+ |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
633 |
+ |
} |
634 |
+ |
|
635 |
+ |
/** |
636 |
|
* Returns a new started daemon Thread running the given runnable. |
637 |
|
*/ |
638 |
|
Thread newStartedThread(Runnable runnable) { |
774 |
|
|
775 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
776 |
|
return new CheckedCallable<String>() { |
777 |
< |
public String realCall() { |
777 |
> |
protected String realCall() { |
778 |
|
try { |
779 |
|
latch.await(); |
780 |
|
} catch (InterruptedException quittingTime) {} |