Skip to content

Commit

Permalink
DaemonThreadFactory (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
turikhay authored Jan 13, 2024
1 parent d80ecc5 commit 63d0adb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 63d0adb

Please sign in to comment.