Changed timing strategy, got ~2x better performance

This commit is contained in:
Brendan Robert 2024-03-15 10:32:01 -05:00
parent f9114248d1
commit b9ae1990e8
1 changed files with 11 additions and 1 deletions

View File

@ -100,10 +100,20 @@ public abstract class IndependentTimedDevice extends TimedDevice {
}
}
public static int SLEEP_PRECISION_LIMIT = 100;
public void sleepUntil(Long time) {
if (time != null) {
while (System.nanoTime() < time) {
Thread.onSpinWait();
int waitTime = (int) ((time - System.nanoTime()) / 1000000);
if (waitTime >= SLEEP_PRECISION_LIMIT) {
try {
Thread.sleep(waitTime);
} catch (InterruptedException ex) {
return;
}
} else {
Thread.onSpinWait();
}
}
}
}