From 63d0adb1c4d9dc979129d0283fb13bc8fea9dd2b Mon Sep 17 00:00:00 2001 From: Artur Khusainov Date: Sat, 13 Jan 2024 22:30:29 +0300 Subject: [PATCH] DaemonThreadFactory (#77) --- .../bungee/MapModCompanion.java | 2 +- .../mapmodcompanion/DaemonThreadFactory.java | 41 +++++++++++++++++++ .../mapmodcompanion/FileChangeWatchdog.java | 7 ++-- .../spigot/MapModCompanion.java | 2 +- .../mapmodcompanion/spigot/XaeroHandler.java | 14 +++---- .../velocity/MapModCompanion.java | 2 +- 6 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 common/src/main/java/com/turikhay/mc/mapmodcompanion/DaemonThreadFactory.java diff --git a/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/MapModCompanion.java b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/MapModCompanion.java index 3649196..79ba373 100644 --- a/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/MapModCompanion.java +++ b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/MapModCompanion.java @@ -73,7 +73,7 @@ public void onLoad() { @Override public void onEnable() { - fileChangeWatchdogScheduler = FileChangeWatchdog.createScheduler(); + fileChangeWatchdogScheduler = FileChangeWatchdog.createScheduler(ILogger.ofJava(logger)); new Metrics(this, BSTATS_ID); load(); } diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/DaemonThreadFactory.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/DaemonThreadFactory.java new file mode 100644 index 0000000..c6513ce --- /dev/null +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/DaemonThreadFactory.java @@ -0,0 +1,41 @@ +package com.turikhay.mc.mapmodcompanion; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +public class DaemonThreadFactory implements ThreadFactory { + private final AtomicInteger counter = new AtomicInteger(); + private final ILogger logger; + private final String name; + private final Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() { + @Override + public void uncaughtException(Thread thread, Throwable throwable) { + logger.error("Error executing the task inside " + thread.getName(), throwable); + } + }; + + public DaemonThreadFactory(ILogger logger, String name) { + this.logger = logger; + this.name = name; + } + + public DaemonThreadFactory(ILogger logger, Class cl) { + this(logger, cl.getSimpleName()); + } + + @Override + public Thread newThread(Runnable runnable) { + Thread t = new Thread(runnable, computeNextName()); + t.setDaemon(true); + t.setUncaughtExceptionHandler(handler); + return t; + } + + private String computeNextName() { + int i = counter.getAndIncrement(); + if (i == 0) { + return name; + } + return name + "#" + i; + } +} diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/FileChangeWatchdog.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/FileChangeWatchdog.java index fa1dcdf..9fc6efe 100644 --- a/common/src/main/java/com/turikhay/mc/mapmodcompanion/FileChangeWatchdog.java +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/FileChangeWatchdog.java @@ -65,9 +65,10 @@ public void cleanUp() { task.cancel(true); } - public static ScheduledThreadPoolExecutor createScheduler() { - ScheduledThreadPoolExecutor service = new ScheduledThreadPoolExecutor(1, r -> - new Thread(r, "MapModCompanion-" + FileChangeWatchdog.class.getSimpleName()) + public static ScheduledThreadPoolExecutor createScheduler(ILogger logger) { + ScheduledThreadPoolExecutor service = new ScheduledThreadPoolExecutor( + 1, + new DaemonThreadFactory(logger, "MapModCompanion-" + FileChangeWatchdog.class.getSimpleName()) ); service.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); return service; diff --git a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/MapModCompanion.java b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/MapModCompanion.java index b29c847..9c61653 100644 --- a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/MapModCompanion.java +++ b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/MapModCompanion.java @@ -63,7 +63,7 @@ public void onLoad() { @Override public void onEnable() { - fileChangeWatchdogScheduler = FileChangeWatchdog.createScheduler(); + fileChangeWatchdogScheduler = FileChangeWatchdog.createScheduler(ILogger.ofJava(logger)); new Metrics(this, BSTATS_ID); saveDefaultConfig(); load(); diff --git a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/XaeroHandler.java b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/XaeroHandler.java index 25605c1..83f8c16 100644 --- a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/XaeroHandler.java +++ b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/XaeroHandler.java @@ -1,9 +1,6 @@ package com.turikhay.mc.mapmodcompanion.spigot; -import com.turikhay.mc.mapmodcompanion.Handler; -import com.turikhay.mc.mapmodcompanion.InitializationException; -import com.turikhay.mc.mapmodcompanion.LevelMapProperties; -import com.turikhay.mc.mapmodcompanion.PrefixLogger; +import com.turikhay.mc.mapmodcompanion.*; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -23,20 +20,21 @@ import java.util.logging.Logger; public class XaeroHandler implements Handler, Listener { - private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor( - runnable -> new Thread(runnable, XaeroHandler.class.getSimpleName()) - ); - private final Logger logger; private final String configPath; private final String channelName; private final MapModCompanion plugin; + private final ScheduledExecutorService scheduler; public XaeroHandler(Logger logger, String configPath, String channelName, MapModCompanion plugin) { this.logger = logger; this.configPath = configPath; this.channelName = channelName; this.plugin = plugin; + + this.scheduler = Executors.newSingleThreadScheduledExecutor( + new DaemonThreadFactory(ILogger.ofJava(logger), XaeroHandler.class) + ); } public void init() throws InitializationException { diff --git a/velocity/src/main/java/com/turikhay/mc/mapmodcompanion/velocity/MapModCompanion.java b/velocity/src/main/java/com/turikhay/mc/mapmodcompanion/velocity/MapModCompanion.java index c7b22fe..d5a17b9 100644 --- a/velocity/src/main/java/com/turikhay/mc/mapmodcompanion/velocity/MapModCompanion.java +++ b/velocity/src/main/java/com/turikhay/mc/mapmodcompanion/velocity/MapModCompanion.java @@ -100,7 +100,7 @@ public IdLookup getConverter() { @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { - fileChangeWatchdogScheduler = FileChangeWatchdog.createScheduler(); + fileChangeWatchdogScheduler = FileChangeWatchdog.createScheduler(ofSlf4j(logger)); metricsFactory.make(this, BSTATS_ID); load(); }