From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Tue, 23 Oct 2018 23:14:38 -0400 Subject: [PATCH] Improve Server Thread Pool and Thread Priorities Use a simple executor since Fork join is a much more complex pool type and we are not using its capabilities. Set thread priorities so main thread has above normal priority over server threads Allow usage of a single thread executor by not using ForkJoin so single core CPU's. diff --git a/src/main/java/net/minecraft/SystemUtils.java b/src/main/java/net/minecraft/SystemUtils.java index 68ce7605bd63ea280b96db8230463d2afb0a6cb1..c7cbf132bfcb5c5e49b269ebfe5ef9a049ff327f 100644 --- a/src/main/java/net/minecraft/SystemUtils.java +++ b/src/main/java/net/minecraft/SystemUtils.java @@ -54,8 +54,8 @@ import org.apache.logging.log4j.Logger; public class SystemUtils { private static final AtomicInteger c = new AtomicInteger(1); - private static final ExecutorService d = a("Bootstrap"); - private static final ExecutorService e = a("Main"); + private static final ExecutorService d = a("Bootstrap", -2); // Paper - add -2 priority + private static final ExecutorService e = a("Main", -1); // Paper - add -1 priority private static final ExecutorService f = n(); public static LongSupplier a = System::nanoTime; public static final UUID b = new UUID(0L, 0L); public static final UUID getNullUUID() {return b;} // Paper OBFHELPER @@ -85,15 +85,18 @@ public class SystemUtils { return Instant.now().toEpochMilli(); } - private static ExecutorService a(String s) { - int i = MathHelper.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, 7); - Object object; + private static ExecutorService a(String s, int priorityModifier) { // Paper - add priority + // Paper start - use simpler thread pool that allows 1 thread + int i = Math.min(8, Math.max(Runtime.getRuntime().availableProcessors() - 2, 1)); + i = Integer.getInteger("Paper.WorkerThreadCount", i); + ExecutorService object; if (i <= 0) { object = MoreExecutors.newDirectExecutorService(); } else { - object = new ForkJoinPool(i, (forkjoinpool) -> { - ForkJoinWorkerThread forkjoinworkerthread = new ForkJoinWorkerThread(forkjoinpool) { + object = new java.util.concurrent.ThreadPoolExecutor(i, i,0L, TimeUnit.MILLISECONDS, new java.util.concurrent.LinkedBlockingQueue(), target -> new ServerWorkerThread(target, s, priorityModifier)); + } + /* protected void onTermination(Throwable throwable) { if (throwable != null) { SystemUtils.LOGGER.warn("{} died", this.getName(), throwable); @@ -109,6 +112,7 @@ public class SystemUtils { return forkjoinworkerthread; }, SystemUtils::a, true); } + }*/ // Paper end return (ExecutorService) object; } @@ -157,6 +161,7 @@ public class SystemUtils { }); } + public static void onThreadError(Thread thread, Throwable throwable) { a(thread, throwable); } // Paper - OBFHELPER private static void a(Thread thread, Throwable throwable) { c(throwable); if (throwable instanceof CompletionException) { diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index aa5abee5dd7dc601f3f738f11faf434d90e493a4..38ddb77c4b979cd22e4ec83ca109955d11739f1f 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -284,6 +284,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant