Skip to content

Commit

Permalink
Use new ThreadPoolExecutor to create executor of VT scheduler
Browse files Browse the repository at this point in the history
This probably can avoid high CPU usage when executing plugin's high frequency tasks
  • Loading branch information
Dreeam-qwq committed Oct 8, 2024
1 parent bc80a3e commit b4dbdda
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions patches/server/0066-Virtual-Thread-for-async-scheduler.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Subject: [PATCH] Virtual Thread for async scheduler


diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java
index 3c1992e212a6d6f1db4d5b807b38d71913619fc0..fe4f1868a5baebceb9ad0520f059ac8c4a68d397 100644
index 3c1992e212a6d6f1db4d5b807b38d71913619fc0..e9168902552cc6640db3a432dbd9e695f2e3ec9c 100644
--- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java
+++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java
@@ -38,17 +38,30 @@ import java.util.concurrent.TimeUnit;
@@ -38,17 +38,40 @@ import java.util.concurrent.TimeUnit;

public class CraftAsyncScheduler extends CraftScheduler {

Expand All @@ -28,13 +28,23 @@ index 3c1992e212a6d6f1db4d5b807b38d71913619fc0..fe4f1868a5baebceb9ad0520f059ac8c
+
+ // Leaf start - Ability to use Virtual Thread for async scheduler
+ if (org.dreeam.leaf.config.modules.opt.VT4BukkitScheduler.enabled) {
+ executor = Executors.newThreadPerTaskExecutor(Thread.ofVirtual().name("Craft Scheduler Thread - ", 0).factory());
+ executor = new ThreadPoolExecutor(
+ 4, Integer.MAX_VALUE, 10L, TimeUnit.SECONDS, new SynchronousQueue<>(), // Use 10s for keepalive time
+ Thread.ofVirtual()
+ .name("Craft Scheduler Thread - ", 0)
+ .uncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(net.minecraft.server.MinecraftServer.LOGGER))
+ .factory()
+ );
+
+ return;
+ }
+
+ executor = new ThreadPoolExecutor(
+ 4, Integer.MAX_VALUE, 30L, TimeUnit.SECONDS, new SynchronousQueue<>(),
+ new ThreadFactoryBuilder().setNameFormat("Craft Scheduler Thread - %1$d").setUncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(net.minecraft.server.MinecraftServer.LOGGER)).build());
+ new ThreadFactoryBuilder()
+ .setNameFormat("Craft Scheduler Thread - %1$d")
+ .setUncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(net.minecraft.server.MinecraftServer.LOGGER))
+ .build());
+
+ var threadPoolExecutor = (ThreadPoolExecutor) executor;
+
Expand Down

0 comments on commit b4dbdda

Please sign in to comment.