Clean up World Gen Threads after termination
This commit is contained in:
+10
-2
@@ -18,6 +18,7 @@ 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.RateLimitedThreadPoolExecutor;
|
||||
import com.seibel.distanthorizons.core.util.objects.UncheckedInterruptedException;
|
||||
import com.seibel.distanthorizons.core.util.LodUtil;
|
||||
import com.seibel.distanthorizons.core.wrapperInterfaces.IWrapperFactory;
|
||||
@@ -34,7 +35,7 @@ public class WorldGenerationQueue implements Closeable, IDebugRenderable
|
||||
{
|
||||
private static final Logger LOGGER = DhLoggerBuilder.getLogger();
|
||||
|
||||
public static final DhThreadFactory THREAD_FACTORY = new DhThreadFactory(ThreadUtil.THREAD_NAME_PREFIX + "Gen-Worker-Thread", Thread.MIN_PRIORITY);
|
||||
public static final DhThreadFactory THREAD_FACTORY = new DhThreadFactory(ThreadUtil.THREAD_NAME_PREFIX + "World-Gen-Worker-Thread", Thread.MIN_PRIORITY);
|
||||
|
||||
private final IDhApiWorldGenerator generator;
|
||||
|
||||
@@ -73,7 +74,7 @@ public class WorldGenerationQueue implements Closeable, IDebugRenderable
|
||||
private final HashMap<DhLodPos, StackTraceElement[]> alreadyGeneratedPosHashSet = new HashMap<>(MAX_ALREADY_GENERATED_COUNT);
|
||||
private final Queue<DhLodPos> alreadyGeneratedPosQueue = new LinkedList<>();
|
||||
|
||||
private static ExecutorService worldGeneratorThreadPool;
|
||||
private static RateLimitedThreadPoolExecutor worldGeneratorThreadPool;
|
||||
private static ConfigChangeListener<Integer> configListener;
|
||||
|
||||
|
||||
@@ -527,6 +528,7 @@ public class WorldGenerationQueue implements Closeable, IDebugRenderable
|
||||
}
|
||||
|
||||
worldGeneratorThreadPool = ThreadUtil.makeRateLimitedThreadPool(threadPoolSize, THREAD_FACTORY, Config.Client.Advanced.MultiThreading.runTimeRatioForWorldGenerationThreads);
|
||||
worldGeneratorThreadPool.setOnTerminatedEventHandler(WorldGenerationQueue::onWorldGenThreadPoolTerminated);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -542,6 +544,12 @@ public class WorldGenerationQueue implements Closeable, IDebugRenderable
|
||||
}
|
||||
}
|
||||
|
||||
private static void onWorldGenThreadPoolTerminated()
|
||||
{
|
||||
LOGGER.debug("World generator thread pool terminated. Suggesting the JVM runs a garbage collection to clean up any loose world generation objects...");
|
||||
System.gc();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//=========//
|
||||
|
||||
+21
@@ -15,6 +15,8 @@ public class RateLimitedThreadPoolExecutor extends ThreadPoolExecutor
|
||||
/** How long it took this thread to run its last task */
|
||||
private final ThreadLocal<Long> lastRunDurationNanoTimeRef = ThreadLocal.withInitial(() -> -1L);
|
||||
|
||||
private Runnable onTerminatedEventHandler = null;
|
||||
|
||||
|
||||
|
||||
//==============//
|
||||
@@ -63,4 +65,23 @@ public class RateLimitedThreadPoolExecutor extends ThreadPoolExecutor
|
||||
this.lastRunDurationNanoTimeRef.set(System.nanoTime() - this.runStartNanoTimeRef.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void terminated()
|
||||
{
|
||||
super.terminated();
|
||||
if (this.onTerminatedEventHandler != null)
|
||||
{
|
||||
this.onTerminatedEventHandler.run();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//==============//
|
||||
// custom logic //
|
||||
//==============//
|
||||
|
||||
/** only one event handler can be present at a time */
|
||||
public void setOnTerminatedEventHandler(Runnable runnable) { this.onTerminatedEventHandler = runnable; }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user