Skip to content

Commit

Permalink
Merge pull request #92 from emustudio/feature-78
Browse files Browse the repository at this point in the history
Sleep improvements
  • Loading branch information
vbmacher committed Dec 29, 2023
2 parents 8db6e0f + 1d39d9b commit c073a79
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SleepUtils {

static {
// determine sleep precision
int count = 100;
int count = 600;
long time = 0;
for (int i = 0; i < count; i++) {
long start = System.nanoTime();
Expand All @@ -40,6 +40,9 @@ public class SleepUtils {
}
SLEEP_PRECISION = time / count;
SPIN_YIELD_PRECISION = SLEEP_PRECISION / 2;

System.out.println("Sleep precision: " + SLEEP_PRECISION + " ns");
System.out.println("Spin yield precision: " + SPIN_YIELD_PRECISION + " ns");
}


Expand Down Expand Up @@ -68,7 +71,7 @@ public static void preciseSleepNanos(long nanoDuration) {
Thread.currentThread().interrupt();
}
} else if (timeLeft > SPIN_YIELD_PRECISION) {
Thread.yield();
Thread.onSpinWait();
}
timeLeft = end - System.nanoTime();
} while (!Thread.currentThread().isInterrupted() && timeLeft > 0);
Expand Down

0 comments on commit c073a79

Please sign in to comment.