From e770943fc9f19fe8b60a1c655db867d751099dd2 Mon Sep 17 00:00:00 2001 From: s809 <43530948+s809@users.noreply.github.com> Date: Sat, 4 Jan 2025 19:22:27 +0500 Subject: [PATCH] Improve task prioritization --- .../core/util/threading/PrioritySemaphore.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java b/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java index cc7a3aed5..b4e60053c 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/util/threading/PrioritySemaphore.java @@ -2,9 +2,7 @@ package com.seibel.distanthorizons.core.util.threading; import org.jetbrains.annotations.NotNull; -import java.util.Comparator; import java.util.Random; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.Semaphore; import java.util.concurrent.locks.ReentrantLock; @@ -67,12 +65,10 @@ public class PrioritySemaphore // this has to be outside the try-finally to prevent holding the lock while waiting synchronized (thread) { - // random value between -5 and +5 is used to prevent task starvation - // while still allowing higher priority tasks to run sooner - int priority = executor.priority + this.random.nextInt(11) - 5; + int priority = (int) ((executor.priority + 1) * executor.getTaskCount() * 100000 / executor.getAverageRunTimeInMs()); // this thread will be run when a permit is available - this.queue.put(new ThreadWithPriority(thread,priority)); + this.queue.put(new ThreadWithPriority(thread, priority)); thread.wait(); }