443 |
|
} |
444 |
|
|
445 |
|
/** |
446 |
+ |
* Delays, via Thread.sleep for the given millisecond delay, but |
447 |
+ |
* if the sleep is shorter than specified, may re-sleep or yield |
448 |
+ |
* until time elapses. |
449 |
+ |
*/ |
450 |
+ |
public static void delay(long ms) throws InterruptedException { |
451 |
+ |
long startTime = System.nanoTime(); |
452 |
+ |
long ns = ms * 1000 * 1000; |
453 |
+ |
for (;;) { |
454 |
+ |
if (ms > 0L) |
455 |
+ |
Thread.sleep(ms); |
456 |
+ |
else // too short to sleep |
457 |
+ |
Thread.yield(); |
458 |
+ |
long d = ns - (System.nanoTime() - startTime); |
459 |
+ |
if (d > 0L) |
460 |
+ |
ms = d / (1000 * 1000); |
461 |
+ |
else |
462 |
+ |
break; |
463 |
+ |
} |
464 |
+ |
} |
465 |
+ |
|
466 |
+ |
/** |
467 |
|
* Waits out termination of a thread pool or fails doing so. |
468 |
|
*/ |
469 |
|
public void joinPool(ExecutorService exec) { |
609 |
|
*/ |
610 |
|
void sleep(long millis) { |
611 |
|
try { |
612 |
< |
Thread.sleep(millis); |
612 |
> |
delay(millis); |
613 |
|
} catch (InterruptedException ie) { |
614 |
|
AssertionFailedError afe = |
615 |
|
new AssertionFailedError("Unexpected InterruptedException"); |
844 |
|
|
845 |
|
public class ShortRunnable extends CheckedRunnable { |
846 |
|
protected void realRun() throws Throwable { |
847 |
< |
Thread.sleep(SHORT_DELAY_MS); |
847 |
> |
delay(SHORT_DELAY_MS); |
848 |
|
} |
849 |
|
} |
850 |
|
|
851 |
|
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
852 |
|
protected void realRun() throws InterruptedException { |
853 |
< |
Thread.sleep(SHORT_DELAY_MS); |
853 |
> |
delay(SHORT_DELAY_MS); |
854 |
|
} |
855 |
|
} |
856 |
|
|
857 |
|
public class SmallRunnable extends CheckedRunnable { |
858 |
|
protected void realRun() throws Throwable { |
859 |
< |
Thread.sleep(SMALL_DELAY_MS); |
859 |
> |
delay(SMALL_DELAY_MS); |
860 |
|
} |
861 |
|
} |
862 |
|
|
863 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
864 |
|
protected void realRun() { |
865 |
|
try { |
866 |
< |
Thread.sleep(SMALL_DELAY_MS); |
866 |
> |
delay(SMALL_DELAY_MS); |
867 |
|
} catch (InterruptedException ok) {} |
868 |
|
} |
869 |
|
} |
870 |
|
|
871 |
|
public class SmallCallable extends CheckedCallable { |
872 |
|
protected Object realCall() throws InterruptedException { |
873 |
< |
Thread.sleep(SMALL_DELAY_MS); |
873 |
> |
delay(SMALL_DELAY_MS); |
874 |
|
return Boolean.TRUE; |
875 |
|
} |
876 |
|
} |
877 |
|
|
878 |
|
public class MediumRunnable extends CheckedRunnable { |
879 |
|
protected void realRun() throws Throwable { |
880 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
880 |
> |
delay(MEDIUM_DELAY_MS); |
881 |
|
} |
882 |
|
} |
883 |
|
|
884 |
|
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
885 |
|
protected void realRun() throws InterruptedException { |
886 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
886 |
> |
delay(MEDIUM_DELAY_MS); |
887 |
|
} |
888 |
|
} |
889 |
|
|
891 |
|
return new CheckedRunnable() { |
892 |
|
protected void realRun() { |
893 |
|
try { |
894 |
< |
Thread.sleep(timeoutMillis); |
894 |
> |
delay(timeoutMillis); |
895 |
|
} catch (InterruptedException ok) {} |
896 |
|
}}; |
897 |
|
} |
899 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
900 |
|
protected void realRun() { |
901 |
|
try { |
902 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
902 |
> |
delay(MEDIUM_DELAY_MS); |
903 |
|
} catch (InterruptedException ok) {} |
904 |
|
} |
905 |
|
} |
907 |
|
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
908 |
|
protected void realRun() { |
909 |
|
try { |
910 |
< |
Thread.sleep(LONG_DELAY_MS); |
910 |
> |
delay(LONG_DELAY_MS); |
911 |
|
} catch (InterruptedException ok) {} |
912 |
|
} |
913 |
|
} |
931 |
|
public boolean isDone() { return done; } |
932 |
|
public void run() { |
933 |
|
try { |
934 |
< |
Thread.sleep(timeoutMillis); |
934 |
> |
delay(timeoutMillis); |
935 |
|
done = true; |
936 |
|
} catch (InterruptedException ok) {} |
937 |
|
} |
942 |
|
public volatile boolean done = false; |
943 |
|
public void run() { |
944 |
|
try { |
945 |
< |
Thread.sleep(SHORT_DELAY_MS); |
945 |
> |
delay(SHORT_DELAY_MS); |
946 |
|
done = true; |
947 |
|
} catch (InterruptedException ok) {} |
948 |
|
} |
952 |
|
public volatile boolean done = false; |
953 |
|
public void run() { |
954 |
|
try { |
955 |
< |
Thread.sleep(SMALL_DELAY_MS); |
955 |
> |
delay(SMALL_DELAY_MS); |
956 |
|
done = true; |
957 |
|
} catch (InterruptedException ok) {} |
958 |
|
} |
962 |
|
public volatile boolean done = false; |
963 |
|
public void run() { |
964 |
|
try { |
965 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
965 |
> |
delay(MEDIUM_DELAY_MS); |
966 |
|
done = true; |
967 |
|
} catch (InterruptedException ok) {} |
968 |
|
} |
972 |
|
public volatile boolean done = false; |
973 |
|
public void run() { |
974 |
|
try { |
975 |
< |
Thread.sleep(LONG_DELAY_MS); |
975 |
> |
delay(LONG_DELAY_MS); |
976 |
|
done = true; |
977 |
|
} catch (InterruptedException ok) {} |
978 |
|
} |
989 |
|
public volatile boolean done = false; |
990 |
|
public Object call() { |
991 |
|
try { |
992 |
< |
Thread.sleep(SMALL_DELAY_MS); |
992 |
> |
delay(SMALL_DELAY_MS); |
993 |
|
done = true; |
994 |
|
} catch (InterruptedException ok) {} |
995 |
|
return Boolean.TRUE; |