9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2026-01-04 15:41:40 +00:00

use fixed thread count

This commit is contained in:
hayanesuru
2025-07-06 18:49:33 +09:00
parent 5295b6d3e1
commit 6746bc25a8
2 changed files with 21 additions and 59 deletions

View File

@@ -14,14 +14,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
public class MultithreadedTracker {
@@ -36,10 +29,11 @@ public class MultithreadedTracker {
public static void init() {
if (TRACKER_EXECUTOR == null) {
TRACKER_EXECUTOR = new ThreadPoolExecutor(
getCorePoolSize(),
getMaxPoolSize(),
getKeepAliveTime(), TimeUnit.SECONDS,
getQueueImpl(),
org.dreeam.leaf.config.modules.async.MultithreadedTracker.asyncEntityTrackerThreads,
org.dreeam.leaf.config.modules.async.MultithreadedTracker.asyncEntityTrackerThreads,
0L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(org.dreeam.leaf.config.modules.async.MultithreadedTracker.asyncEntityTrackerQueueSize),
getThreadFactory(),
getRejectedPolicy()
);
@@ -152,24 +146,6 @@ public class MultithreadedTracker {
}
}
private static int getCorePoolSize() {
return 1;
}
private static int getMaxPoolSize() {
return org.dreeam.leaf.config.modules.async.MultithreadedTracker.asyncEntityTrackerMaxThreads;
}
private static long getKeepAliveTime() {
return org.dreeam.leaf.config.modules.async.MultithreadedTracker.asyncEntityTrackerKeepalive;
}
private static BlockingQueue<Runnable> getQueueImpl() {
final int queueCapacity = org.dreeam.leaf.config.modules.async.MultithreadedTracker.asyncEntityTrackerQueueSize;
return new LinkedBlockingQueue<>(queueCapacity);
}
private static @NotNull ThreadFactory getThreadFactory() {
return new ThreadFactoryBuilder()
.setThreadFactory(MultithreadedTrackerThread::new)
@@ -180,26 +156,14 @@ public class MultithreadedTracker {
}
private static @NotNull RejectedExecutionHandler getRejectedPolicy() {
return (rejectedTask, executor) -> {
BlockingQueue<Runnable> workQueue = executor.getQueue();
return (r, executor) -> {
if (!executor.isShutdown()) {
if (!workQueue.isEmpty()) {
List<Runnable> pendingTasks = new ArrayList<>(workQueue.size());
workQueue.drainTo(pendingTasks);
for (Runnable pendingTask : pendingTasks) {
pendingTask.run();
}
executor.getQueue().poll();
executor.execute(r);
if (System.currentTimeMillis() - lastWarnMillis > 30000L) {
LOGGER.warn("Async entity tracker is busy! Oldest tasks will be discard. Increasing threads in Leaf config may help.");
lastWarnMillis = System.currentTimeMillis();
}
rejectedTask.run();
}
if (System.currentTimeMillis() - lastWarnMillis > 30000L) {
LOGGER.warn("Async entity tracker is busy! Tracking tasks will be done in the server thread. Increasing max-threads in Leaf config may help.");
lastWarnMillis = System.currentTimeMillis();
}
};
}

View File

@@ -12,8 +12,7 @@ public class MultithreadedTracker extends ConfigModules {
public static boolean enabled = false;
public static boolean compatModeEnabled = false;
public static int asyncEntityTrackerMaxThreads = 0;
public static int asyncEntityTrackerKeepalive = 60;
public static int asyncEntityTrackerThreads = 0;
public static int asyncEntityTrackerQueueSize = 0;
public static boolean allowRescheduleEvent = true;
private static boolean asyncMultithreadedTrackerInitialized;
@@ -42,22 +41,21 @@ public class MultithreadedTracker extends ConfigModules {
enabled = config.getBoolean(getBasePath() + ".enabled", enabled);
compatModeEnabled = config.getBoolean(getBasePath() + ".compat-mode", compatModeEnabled);
asyncEntityTrackerMaxThreads = config.getInt(getBasePath() + ".max-threads", asyncEntityTrackerMaxThreads);
asyncEntityTrackerKeepalive = config.getInt(getBasePath() + ".keepalive", asyncEntityTrackerKeepalive);
asyncEntityTrackerThreads = config.getInt(getBasePath() + ".threads", asyncEntityTrackerThreads);
asyncEntityTrackerQueueSize = config.getInt(getBasePath() + ".queue-size", asyncEntityTrackerQueueSize);
if (asyncEntityTrackerMaxThreads < 0)
asyncEntityTrackerMaxThreads = Math.max(Runtime.getRuntime().availableProcessors() + asyncEntityTrackerMaxThreads, 1);
else if (asyncEntityTrackerMaxThreads == 0)
asyncEntityTrackerMaxThreads = 1;
if (asyncEntityTrackerThreads < 0)
asyncEntityTrackerThreads = Math.max(Runtime.getRuntime().availableProcessors() + asyncEntityTrackerThreads, 1);
else if (asyncEntityTrackerThreads == 0)
asyncEntityTrackerThreads = 1;
if (!enabled)
asyncEntityTrackerMaxThreads = 0;
asyncEntityTrackerThreads = 0;
else
LeafConfig.LOGGER.info("Using {} threads for Async Entity Tracker", asyncEntityTrackerMaxThreads);
LeafConfig.LOGGER.info("Using {} threads for Async Entity Tracker", asyncEntityTrackerThreads);
if (asyncEntityTrackerQueueSize <= 0)
asyncEntityTrackerQueueSize = asyncEntityTrackerMaxThreads * 384;
asyncEntityTrackerQueueSize = 256;
if (enabled) {
org.dreeam.leaf.async.tracker.MultithreadedTracker.init();