diff --git a/src/main/java/com/seibel/lod/core/util/LodThreadFactory.java b/src/main/java/com/seibel/lod/core/util/LodThreadFactory.java index e9791c349..1585351f2 100644 --- a/src/main/java/com/seibel/lod/core/util/LodThreadFactory.java +++ b/src/main/java/com/seibel/lod/core/util/LodThreadFactory.java @@ -19,8 +19,12 @@ package com.seibel.lod.core.util; +import java.lang.ref.WeakReference; +import java.util.LinkedList; import java.util.concurrent.ThreadFactory; +import com.seibel.lod.core.api.ClientApi; + /** * Just a simple ThreadFactory to name ExecutorService * threads, which can be helpful when debugging. @@ -32,6 +36,7 @@ public class LodThreadFactory implements ThreadFactory public final String threadName; public final int priority; private int threadCount = 0; + private LinkedList> threads = new LinkedList>(); public LodThreadFactory(String newThreadName, int priority) @@ -46,7 +51,34 @@ public class LodThreadFactory implements ThreadFactory { Thread t = new Thread(r, threadName + "[" + (threadCount++) + "]"); t.setPriority(priority); + threads.add(new WeakReference(t)); return t; } + private static String StackTraceToString(StackTraceElement[] e) { + StringBuilder str = new StringBuilder(); + str.append(e[0]); + str.append('\n'); + for (int i = 1; i tRef : threads) { + Thread t = tRef.get(); + if (t != null) { + StackTraceElement[] stacks = t.getStackTrace(); + if (stacks.length != 0) { + ClientApi.LOGGER.info("===========================================\n" + + "Thread: "+t.getName()+"\n"+StackTraceToString(stacks)); + } + } + } + threads.removeIf((weakRef) -> {return weakRef.get() == null;}); + } + }