First of all, Clock
is a really simple class with little implementation and few uses in the standard API. SystemClock
, OffsetClock
and TickClock
are examples in the same file. So the answer seems to be No, it is not in the default API.
If you are given the "slowed-down clock" and may not modify it to add the desired sleepUntil()
-code, and it is slowed down by a fixed amount, you could do something like
public static void sleepUntil(Instant instant, Clock clock)
throws InterruptedException {
Instant now = Instant.now(clock);
long duration = now.until(instant, ChronoUnit.MILLIS);
long testSleep
= ( duration < 10000 )? (duration / 10) : 1000;
long start = clock.millis();
Thread.sleep(testSleep);
long stop = clock.millis();
double clockskew = (stop - start) / testSleep;
now = Instant.now(clock);
Thread.sleep(now.until(instant, ChronoUnit.MILLIS) * clockskew);
}
This first does a test run to determine the clock skew, then sleeps an adjusted duration.
(Maybe you need to adjust the instant in sleep
to adjust to the other clock. Post your clock code if you wish to have it tested/adjusted.)
TimeUnit
and call itssleep
? – TrackClock
repeatedly, there isn’t even a hard way to implement what you want. – Consolute