2020-06-02 08:19:07 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
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/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2020-08-25 04:22:08 +02:00
index 64bf641e0c4e168526c56dd46a3dca7fb09a8d3f..d3349c2654537f0308800e5e464f0d3fb2b1261d 100644
2020-06-02 08:19:07 +02:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2020-06-26 18:20:03 +02:00
@@ -175,6 +175,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2020-06-25 16:09:55 +02:00
S s0 = function.apply(thread); // CraftBukkit - decompile error
2020-06-02 08:19:07 +02:00
2020-06-25 16:09:55 +02:00
atomicreference.set(s0);
+ thread.setPriority(Thread.NORM_PRIORITY+2); // Paper - boost priority
thread.start();
return s0;
}
2020-06-02 08:19:07 +02:00
diff --git a/src/main/java/net/minecraft/server/ServerWorkerThread.java b/src/main/java/net/minecraft/server/ServerWorkerThread.java
new file mode 100644
2020-07-01 13:21:57 +02:00
index 0000000000000000000000000000000000000000..703f45da2954272a7b92e2b11087c80a11f0370d
2020-06-02 08:19:07 +02:00
--- /dev/null
+++ b/src/main/java/net/minecraft/server/ServerWorkerThread.java
2020-07-01 13:21:57 +02:00
@@ -0,0 +1,26 @@
2020-06-02 08:19:07 +02:00
+package net.minecraft.server;
+
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class ServerWorkerThread extends Thread {
+ private static final AtomicInteger threadId = new AtomicInteger(1);
+ public ServerWorkerThread(Runnable target) {
+ super(target, "Server-Worker-" + threadId.getAndIncrement());
+ setPriority(Thread.NORM_PRIORITY-1); // Deprioritize over main
2020-07-01 13:21:57 +02:00
+ this.setDaemon(true);
2020-06-02 08:19:07 +02:00
+ this.setUncaughtExceptionHandler((thread, throwable) -> {
2020-07-01 09:32:26 +02:00
+ thread.setDaemon(true);
2020-06-02 08:19:07 +02:00
+ if (throwable instanceof CompletionException) {
+ throwable = throwable.getCause();
+ }
+
+ if (throwable instanceof ReportedException) {
+ DispenserRegistry.a(((ReportedException) throwable).a().e());
+ System.exit(-1);
+ }
+
+ MinecraftServer.LOGGER.error(String.format("Caught exception in thread %s", thread), throwable);
+ });
+ }
+}
diff --git a/src/main/java/net/minecraft/server/SystemUtils.java b/src/main/java/net/minecraft/server/SystemUtils.java
2020-08-25 04:22:08 +02:00
index cc14e4b4609fa7013df666181c0e02a53970c123..391eca025b69ea96f16403459c7908378cf48eff 100644
2020-06-02 08:19:07 +02:00
--- a/src/main/java/net/minecraft/server/SystemUtils.java
+++ b/src/main/java/net/minecraft/server/SystemUtils.java
2020-08-25 04:22:08 +02:00
@@ -80,14 +80,17 @@ public class SystemUtils {
2020-06-02 08:19:07 +02:00
}
2020-06-25 16:09:55 +02:00
private static ExecutorService a(String s) {
2020-06-02 08:19:07 +02:00
- int i = MathHelper.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, 7);
- Object object;
+ // 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<Runnable>(), ServerWorkerThread::new);
+ }
+ /*
protected void onTermination(Throwable throwable) {
if (throwable != null) {
SystemUtils.LOGGER.warn("{} died", this.getName(), throwable);
2020-08-25 04:22:08 +02:00
@@ -103,6 +106,7 @@ public class SystemUtils {
2020-06-25 16:09:55 +02:00
return forkjoinworkerthread;
}, SystemUtils::a, true);
}
2020-06-02 08:19:07 +02:00
+ }*/ // Paper end
return (ExecutorService) object;
}