From ba20df9f7bd9d465ef075752c42369f81bc4e719 Mon Sep 17 00:00:00 2001 From: syeyoung Date: Thu, 17 Nov 2022 22:44:13 +0900 Subject: - Stop the thread. Signed-off-by: syeyoung --- .../syeyoung/dungeonsguide/mod/DungeonsGuide.java | 41 ++++++++++++++++++++++ .../dungeonsguide/mod/commands/CommandDgDebug.java | 2 +- .../mod/discord/rpc/RichPresenceManager.java | 2 +- .../dungeonsguide/mod/dungeon/MapProcessor.java | 4 ++- .../mod/dungeon/roomfinder/DungeonRoom.java | 3 +- .../icefill/RoomProcessorIcePath2.java | 29 ++++++++------- .../features/impl/advanced/FeatureTestPepole.java | 4 +-- .../discord/inviteViewer/PartyInviteViewer.java | 2 +- .../impl/party/playerpreview/api/ApiFetcher.java | 3 +- .../dungeonsguide/mod/stomp/StompClient.java | 3 +- .../dungeonsguide/mod/stomp/StompManager.java | 3 +- 11 files changed, 71 insertions(+), 25 deletions(-) (limited to 'mod') diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java index 52515a38..91541a86 100755 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java @@ -62,6 +62,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; public class DungeonsGuide implements DGInterface { @@ -72,6 +74,36 @@ public class DungeonsGuide implements DGInterface { public boolean verbose = false; private SkyblockStatus skyblockStatus; + @Getter + public static final ThreadGroup THREAD_GROUP = new ThreadGroup("Dungeons Guide"); + + public static final DefaultThreadFactory THREAD_FACTORY = new DefaultThreadFactory(); + + static class DefaultThreadFactory implements ThreadFactory { + private static final AtomicInteger poolNumber = new AtomicInteger(1); + private final ThreadGroup group; + private final AtomicInteger threadNumber = new AtomicInteger(1); + private final String namePrefix; + + DefaultThreadFactory() { + group = THREAD_GROUP; + namePrefix = "pool-" + + poolNumber.getAndIncrement() + + "-thread-"; + } + + public Thread newThread(Runnable r) { + Thread t = new Thread(group, r, + namePrefix + threadNumber.getAndIncrement(), + 0); + if (t.isDaemon()) + t.setDaemon(false); + if (t.getPriority() != Thread.NORM_PRIORITY) + t.setPriority(Thread.NORM_PRIORITY); + return t; + } + } + @Getter private CosmeticsManager cosmeticsManager; @Getter @@ -211,9 +243,18 @@ public class DungeonsGuide implements DGInterface { @Override public void unload() { // have FUN! + + for (Object registeredListener : registeredListeners) { MinecraftForge.EVENT_BUS.unregister(registeredListener); } + THREAD_GROUP.interrupt(); + THREAD_GROUP.stop(); + try { + Thread.sleep(1000); // This is requirement for all the threads to finish within 1 second. or reference leak. + } catch (InterruptedException e) { + } + THREAD_GROUP.destroy(); } @Override diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/commands/CommandDgDebug.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/commands/CommandDgDebug.java index 7df0ed6f..ab69fec6 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/commands/CommandDgDebug.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/commands/CommandDgDebug.java @@ -116,7 +116,7 @@ public class CommandDgDebug extends CommandBase { } - (new Thread(() -> { + (new Thread(DungeonsGuide.THREAD_GROUP, () -> { try { ChatTransmitter.addToQueue("§r§aDungeon starts in 15 seconds.§r", false); Thread.sleep(6000); diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/discord/rpc/RichPresenceManager.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/discord/rpc/RichPresenceManager.java index ebfd00aa..135716f9 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/discord/rpc/RichPresenceManager.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/discord/rpc/RichPresenceManager.java @@ -48,7 +48,7 @@ import java.util.Set; public class RichPresenceManager implements Runnable { public static RichPresenceManager INSTANCE = new RichPresenceManager(); - private Thread t = new Thread(this); + private Thread t = new Thread(DungeonsGuide.THREAD_GROUP, this); public RichPresenceManager() { t.start(); diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/MapProcessor.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/MapProcessor.java index 6ae4b5c2..84adf8a0 100755 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/MapProcessor.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/MapProcessor.java @@ -95,7 +95,9 @@ public class MapProcessor { } - ExecutorService es = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("Dg-MapProcessor-%d").build()); + ExecutorService es = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() + .setThreadFactory(DungeonsGuide.THREAD_FACTORY) + .setNameFormat("Dg-MapProcessor-%d").build()); int processMapThroddle; diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/roomfinder/DungeonRoom.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/roomfinder/DungeonRoom.java index 3d029910..f57e1d24 100755 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/roomfinder/DungeonRoom.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/roomfinder/DungeonRoom.java @@ -22,6 +22,7 @@ import com.google.common.collect.Sets; import kr.syeyoung.dungeonsguide.dungeon.data.DungeonRoomInfo; import kr.syeyoung.dungeonsguide.dungeon.mechanics.DungeonRoomDoor; import kr.syeyoung.dungeonsguide.dungeon.mechanics.dunegonmechanic.DungeonMechanic; +import kr.syeyoung.dungeonsguide.mod.DungeonsGuide; import kr.syeyoung.dungeonsguide.mod.dungeon.DungeonContext; import kr.syeyoung.dungeonsguide.mod.dungeon.MapProcessor; import kr.syeyoung.dungeonsguide.mod.dungeon.doorfinder.DungeonDoor; @@ -152,7 +153,7 @@ public class DungeonRoom { } } - private static final ScheduledExecutorService asyncPathFinder = Executors.newScheduledThreadPool(4); + private static final ScheduledExecutorService asyncPathFinder = Executors.newScheduledThreadPool(4, DungeonsGuide.THREAD_FACTORY); @Getter private final NodeProcessorDungeonRoom nodeProcessorDungeonRoom; diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/roomprocessor/icefill/RoomProcessorIcePath2.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/roomprocessor/icefill/RoomProcessorIcePath2.java index afabd946..b6bba1f0 100755 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/roomprocessor/icefill/RoomProcessorIcePath2.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/dungeon/roomprocessor/icefill/RoomProcessorIcePath2.java @@ -20,6 +20,7 @@ package kr.syeyoung.dungeonsguide.mod.dungeon.roomprocessor.icefill; import kr.syeyoung.dungeonsguide.dungeon.data.OffsetPointSet; +import kr.syeyoung.dungeonsguide.mod.DungeonsGuide; import kr.syeyoung.dungeonsguide.mod.chat.ChatTransmitter; import kr.syeyoung.dungeonsguide.mod.dungeon.roomfinder.DungeonRoom; import kr.syeyoung.dungeonsguide.mod.dungeon.roomprocessor.GeneralRoomProcessor; @@ -68,22 +69,20 @@ public class RoomProcessorIcePath2 extends GeneralRoomProcessor { } } - new Thread() { - public void run() { - List hamiltonianPath = findFirstHamiltonianPath(map, startX, startY, endX, endY); - if (hamiltonianPath == null) { - ChatTransmitter.addToQueue("§eDungeons Guide §7:: §eIcePath §7:: §cCouldn't find solution for floor "+s); - return; - } - hamiltonianPath.add(0,new Point(startX, startY)); - List poses = new LinkedList(); - for (int i = 0; i < hamiltonianPath.size(); i++) { - Point p = hamiltonianPath.get(i); - poses.add(map2[p.y][p.x]); - } - solution.add(poses); + new Thread(DungeonsGuide.THREAD_GROUP, () -> { + List hamiltonianPath = findFirstHamiltonianPath(map, startX, startY, endX, endY); + if (hamiltonianPath == null) { + ChatTransmitter.addToQueue("§eDungeons Guide §7:: §eIcePath §7:: §cCouldn't find solution for floor "+s); + return; } - }.start(); + hamiltonianPath.add(0,new Point(startX, startY)); + List poses = new LinkedList(); + for (int i = 0; i < hamiltonianPath.size(); i++) { + Point p = hamiltonianPath.get(i); + poses.add(map2[p.y][p.x]); + } + solution.add(poses); + }).start(); } catch (Exception e) { e.printStackTrace(); } diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/advanced/FeatureTestPepole.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/advanced/FeatureTestPepole.java index bd495225..4d994ba8 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/advanced/FeatureTestPepole.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/advanced/FeatureTestPepole.java @@ -160,7 +160,7 @@ public class FeatureTestPepole extends GuiFeature implements ChatListener, Dunge - ExecutorService executor = Executors.newFixedThreadPool(5); + ExecutorService executor = Executors.newFixedThreadPool(5, DungeonsGuide.THREAD_FACTORY); public ItemStack getSkullByUserName(String username) { @@ -323,7 +323,7 @@ public class FeatureTestPepole extends GuiFeature implements ChatListener, Dunge if(toDraw == null && !cachedProfiles.containsKey(stack.getName())){ cachedProfiles.put(stack.getName(), false); - new Thread(() -> { + new Thread(DungeonsGuide.THREAD_GROUP, () -> { Map map = Minecraft.getMinecraft().getSkinManager().loadSkinFromCache(stack); if (map.containsKey(MinecraftProfileTexture.Type.SKIN)) { diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/discord/inviteViewer/PartyInviteViewer.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/discord/inviteViewer/PartyInviteViewer.java index 85d45349..20c208e7 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/discord/inviteViewer/PartyInviteViewer.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/discord/inviteViewer/PartyInviteViewer.java @@ -150,7 +150,7 @@ public class PartyInviteViewer extends SimpleFeature implements GuiPostRenderLis public CopyOnWriteArrayList joinRequests = new CopyOnWriteArrayList<>(); - ExecutorService executorService = Executors.newFixedThreadPool(3); + ExecutorService executorService = Executors.newFixedThreadPool(3, DungeonsGuide.THREAD_FACTORY); public Map> futureMap = new HashMap<>(); public Map imageMap = new HashMap<>(); diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/api/ApiFetcher.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/api/ApiFetcher.java index 32c38440..9519879b 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/api/ApiFetcher.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/api/ApiFetcher.java @@ -23,6 +23,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.mojang.authlib.GameProfile; +import kr.syeyoung.dungeonsguide.mod.DungeonsGuide; import kr.syeyoung.dungeonsguide.mod.features.impl.party.playerpreview.api.playerprofile.PlayerProfile; import kr.syeyoung.dungeonsguide.mod.features.impl.party.playerpreview.api.playerprofile.PlayerProfileParser; import kr.syeyoung.dungeonsguide.mod.utils.TextUtils; @@ -50,7 +51,7 @@ public class ApiFetcher { private static final Map> UIDtoNickname = new ConcurrentHashMap<>(); private static final Map> UIDtoGameProfile = new ConcurrentHashMap<>(); - private static final ExecutorService ex = Executors.newFixedThreadPool(4); + private static final ExecutorService ex = Executors.newFixedThreadPool(4, DungeonsGuide.THREAD_FACTORY); private static final Set invalidKeys = new HashSet<>(); diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/stomp/StompClient.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/stomp/StompClient.java index d23debc2..3bae2eda 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/stomp/StompClient.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/stomp/StompClient.java @@ -18,6 +18,7 @@ package kr.syeyoung.dungeonsguide.mod.stomp; +import kr.syeyoung.dungeonsguide.mod.DungeonsGuide; import lombok.Getter; import net.minecraftforge.common.MinecraftForge; import org.apache.logging.log4j.LogManager; @@ -61,7 +62,7 @@ public class StompClient extends WebSocketClient { private ScheduledFuture heartbeat = null; - private static final ScheduledExecutorService ex = Executors.newScheduledThreadPool(1); + private static final ScheduledExecutorService ex = Executors.newScheduledThreadPool(1, DungeonsGuide.THREAD_FACTORY); @Override public void onOpen(ServerHandshake handshakedata) { send(new StompPayload().method(StompHeader.CONNECT) diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/stomp/StompManager.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/stomp/StompManager.java index d430e586..a06f5e32 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/stomp/StompManager.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/stomp/StompManager.java @@ -2,6 +2,7 @@ package kr.syeyoung.dungeonsguide.mod.stomp; import com.google.common.base.Throwables; import kr.syeyoung.dungeonsguide.launcher.auth.AuthManager; +import kr.syeyoung.dungeonsguide.mod.DungeonsGuide; import kr.syeyoung.dungeonsguide.mod.events.impl.StompConnectedEvent; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -45,7 +46,7 @@ public class StompManager { } } - ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor(); + ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor(DungeonsGuide.THREAD_FACTORY); public void onStompDied(StompDiedEvent event) { logger.info("Stomp Connection closed, trying to reconnect - {} - {}", event.reason, event.code); -- cgit