mirror of
https://github.com/Jozufozu/Flywheel.git
synced 2024-12-27 07:26:48 +01:00
Hol up
- Spin wait for .01ms in WorkerThreads before waiting
This commit is contained in:
parent
71e6dcc9ef
commit
3222c969e5
1 changed files with 18 additions and 5 deletions
|
@ -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;
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
processTask(task);
|
Thread.onSpinWait();
|
||||||
}
|
}
|
||||||
|
taskNotifier.awaitNotification();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue