- Spin wait for .01ms in WorkerThreads before waiting
This commit is contained in:
Jozufozu 2023-05-27 12:01:47 -07:00
parent 71e6dcc9ef
commit 3222c969e5

View file

@ -198,7 +198,6 @@ public class ParallelTaskExecutor implements TaskExecutor {
} }
private class WorkerThread extends Thread { private class WorkerThread extends Thread {
public WorkerThread(String name) { public WorkerThread(String name) {
super(name); super(name);
} }
@ -209,14 +208,28 @@ public class ParallelTaskExecutor implements TaskExecutor {
while (ParallelTaskExecutor.this.running.get()) { while (ParallelTaskExecutor.this.running.get()) {
Runnable task = taskQueue.pollFirst(); Runnable task = taskQueue.pollFirst();
if (task == null) { if (task != null) {
processTask(task);
} else {
// Nothing to do, time to sleep. // Nothing to do, time to sleep.
taskNotifier.awaitNotification(); spinThenWait();
continue; }
}
} }
processTask(task); private void spinThenWait() {
} var waitStart = System.nanoTime();
// Spin for .01ms before waiting to reduce latency in narrow conditions.
while (System.nanoTime() - waitStart < 10_000) {
if (!taskQueue.isEmpty()) {
// Nice! Exit without waiting.
return;
}
Thread.onSpinWait();
}
taskNotifier.awaitNotification();
} }
} }
} }