Fix WorldGen ThreadFactory not being used

This commit is contained in:
James Seibel
2023-08-18 08:25:15 -05:00
parent c7ac9faccd
commit 91f67cac9b
2 changed files with 24 additions and 7 deletions
@@ -17,6 +17,7 @@ import com.seibel.distanthorizons.core.file.fullDatafile.FullDataFileHandler;
import com.seibel.distanthorizons.core.render.renderer.DebugRenderer;
import com.seibel.distanthorizons.core.render.renderer.IDebugRenderable;
import com.seibel.distanthorizons.core.util.ThreadUtil;
import com.seibel.distanthorizons.core.util.objects.DhThreadFactory;
import com.seibel.distanthorizons.core.util.objects.UncheckedInterruptedException;
import com.seibel.distanthorizons.core.util.LodUtil;
import com.seibel.distanthorizons.core.wrapperInterfaces.IWrapperFactory;
@@ -496,6 +497,8 @@ public class WorldGenerationQueue implements Closeable, IDebugRenderable
// executor handler methods //
//==========================//
public static final DhThreadFactory THREAD_FACTORY = new DhThreadFactory("Gen-Worker-Thread", Thread.MIN_PRIORITY);
/**
* Creates a new executor. <br>
* Does nothing if an executor already exists.
@@ -523,7 +526,7 @@ public class WorldGenerationQueue implements Closeable, IDebugRenderable
worldGeneratorThreadPool.shutdown();
}
worldGeneratorThreadPool = ThreadUtil.makeRateLimitedThreadPool(threadPoolSize, "DH-Gen-Worker-Thread", Thread.MIN_PRIORITY, Config.Client.Advanced.MultiThreading.runTimeRatioForWorldGenerationThreads);
worldGeneratorThreadPool = ThreadUtil.makeRateLimitedThreadPool(threadPoolSize, THREAD_FACTORY, Config.Client.Advanced.MultiThreading.runTimeRatioForWorldGenerationThreads);
}
/**
@@ -20,30 +20,44 @@ public class ThreadUtil
// create rate limited thread pool //
public static RateLimitedThreadPoolExecutor makeRateLimitedThreadPool(int poolSize, String name, ConfigEntry<Double> runTimeRatioConfigEntry) { return makeRateLimitedThreadPool(poolSize, name, DEFAULT_RELATIVE_PRIORITY, runTimeRatioConfigEntry); }
public static RateLimitedThreadPoolExecutor makeRateLimitedThreadPool(int poolSize, String name, ConfigEntry<Double> runTimeRatioConfigEntry)
{
return makeRateLimitedThreadPool(poolSize, name, DEFAULT_RELATIVE_PRIORITY, runTimeRatioConfigEntry);
}
public static RateLimitedThreadPoolExecutor makeRateLimitedThreadPool(int poolSize, String name, int relativePriority, ConfigEntry<Double> runTimeRatioConfigEntry)
{
DhThreadFactory threadFactory = new DhThreadFactory("DH-" + name, Thread.NORM_PRIORITY + relativePriority);
return makeRateLimitedThreadPool(poolSize, threadFactory, runTimeRatioConfigEntry);
}
public static RateLimitedThreadPoolExecutor makeRateLimitedThreadPool(int poolSize, DhThreadFactory threadFactory, ConfigEntry<Double> runTimeRatioConfigEntry)
{
// remove the old listener if one exists
if (THREAD_CHANGE_LISTENERS_BY_THREAD_NAME.containsKey(name))
if (THREAD_CHANGE_LISTENERS_BY_THREAD_NAME.containsKey(threadFactory.threadName))
{
// note: this assumes only one thread pool exists with a given name
THREAD_CHANGE_LISTENERS_BY_THREAD_NAME.get(name).close();
THREAD_CHANGE_LISTENERS_BY_THREAD_NAME.remove(name);
THREAD_CHANGE_LISTENERS_BY_THREAD_NAME.get(threadFactory.threadName).close();
THREAD_CHANGE_LISTENERS_BY_THREAD_NAME.remove(threadFactory.threadName);
}
RateLimitedThreadPoolExecutor executor = makeRateLimitedThreadPool(poolSize, name, runTimeRatioConfigEntry.get(), relativePriority);
RateLimitedThreadPoolExecutor executor = makeRateLimitedThreadPool(poolSize, runTimeRatioConfigEntry.get(), threadFactory);
ConfigChangeListener<Double> changeListener = new ConfigChangeListener<>(runTimeRatioConfigEntry, (newRunTimeRatio) -> { executor.runTimeRatio = newRunTimeRatio; });
THREAD_CHANGE_LISTENERS_BY_THREAD_NAME.put(name, changeListener);
THREAD_CHANGE_LISTENERS_BY_THREAD_NAME.put(threadFactory.threadName, changeListener);
return executor;
}
/** should only be used if there isn't a config controlling the run time ratio of this thread pool */
public static RateLimitedThreadPoolExecutor makeRateLimitedThreadPool(int poolSize, String name, Double runTimeRatio, int relativePriority)
{
return new RateLimitedThreadPoolExecutor(poolSize, runTimeRatio, new DhThreadFactory("DH-" + name, Thread.NORM_PRIORITY + relativePriority));
}
public static RateLimitedThreadPoolExecutor makeRateLimitedThreadPool(int poolSize, Double runTimeRatio, DhThreadFactory threadFactory)
{
return new RateLimitedThreadPoolExecutor(poolSize, runTimeRatio, threadFactory);
}
// create thread pool //