9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-23 08:59:23 +00:00
Files
Leaf/patches/server/0048-MikuServer-Add-shutdown-hook-to-async-executor-and-r.patch
2023-01-30 03:26:45 -05:00

54 lines
2.4 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: wangxyper <wangxyper@163.com>
Date: Sat, 28 Jan 2023 12:51:08 +0800
Subject: [PATCH] MikuServer: Add shutdown hook to async executor and refactor
thread id counter
Original license: MIT
Original project: https://github.com/MikuMC/MikuServer
diff --git a/src/main/java/co/mikumc/mikuserver/PublicConstants.java b/src/main/java/co/mikumc/mikuserver/PublicConstants.java
index c6da0ce6b7181d3fb9b6ea8c2a600741d92e8f18..3f2e2e2be1d53b9257f99fb19a50491efc629925 100644
--- a/src/main/java/co/mikumc/mikuserver/PublicConstants.java
+++ b/src/main/java/co/mikumc/mikuserver/PublicConstants.java
@@ -8,17 +8,22 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class PublicConstants {
- public static final AtomicInteger threadId = new AtomicInteger();
+ private static final AtomicInteger asyncWorkerThreadIdCounter = new AtomicInteger();
public static final CallbackExecutor asyncExecutor = new CallbackExecutor(
Runtime.getRuntime().availableProcessors(),
Runtime.getRuntime().availableProcessors(),
50,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
- task -> {
- AnotherTickThread worker = new AnotherTickThread(task,"MikuServer-Async-Worker # "+threadId.getAndIncrement());
- worker.setDaemon(true);
- return worker;
- }
+ task -> new AnotherTickThread(task,"MikuServer-Async-Worker # "+ asyncWorkerThreadIdCounter.getAndIncrement())
);
+
+ static {
+ Runtime.getRuntime().addShutdownHook(new Thread(()->{
+ if (asyncExecutor.isSubmittingStarted()){
+ asyncExecutor.stopSubmitting();
+ }
+ asyncExecutor.shutdownNow();
+ }));
+ }
}
diff --git a/src/main/java/co/mikumc/mikuserver/utils/AnotherTickThread.java b/src/main/java/co/mikumc/mikuserver/utils/AnotherTickThread.java
index 8dd4423fb1477c901e88c07537ca736eb4f9a8cc..b3ea36c67e9eb5dafd1c7ff5abdf1233dfbd562b 100644
--- a/src/main/java/co/mikumc/mikuserver/utils/AnotherTickThread.java
+++ b/src/main/java/co/mikumc/mikuserver/utils/AnotherTickThread.java
@@ -9,5 +9,6 @@ public class AnotherTickThread extends TickThread {
public AnotherTickThread(Runnable run, String name) {
super(run, name);
+ this.setPriority(Thread.NORM_PRIORITY - 2);
}
}