From d373b806ed0a4319b687a19a56973186327fa955 Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Fri, 31 Jan 2025 18:39:05 +1000 Subject: [PATCH] Replace spinlock with a wait loop, freeing up a cpu thread during import --- .../voxy/commonImpl/importers/WorldImporter.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/cortex/voxy/commonImpl/importers/WorldImporter.java b/src/main/java/me/cortex/voxy/commonImpl/importers/WorldImporter.java index bb8bc4da..736a6d29 100644 --- a/src/main/java/me/cortex/voxy/commonImpl/importers/WorldImporter.java +++ b/src/main/java/me/cortex/voxy/commonImpl/importers/WorldImporter.java @@ -159,7 +159,11 @@ public class WorldImporter { throw new RuntimeException(e); } while ((this.totalChunks.get()-this.chunksProcessed.get() > 10_000) && this.isRunning) { - Thread.onSpinWait(); + try { + Thread.sleep(1); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } } if (!this.isRunning) { onCompletion.accept(this.totalChunks.get()); @@ -168,7 +172,12 @@ public class WorldImporter { } this.threadPool.blockTillEmpty(); while (this.chunksProcessed.get() != this.totalChunks.get() && this.isRunning) { - Thread.onSpinWait(); + Thread.yield(); + try { + Thread.sleep(10); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } } onCompletion.accept(this.totalChunks.get()); this.worker = null;