Add stack dump for threadFactory

This commit is contained in:
tom lee
2022-01-31 00:23:15 +08:00
parent 99ca5f6bc6
commit 834cfe2e10
@@ -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<WeakReference<Thread>> threads = new LinkedList<WeakReference<Thread>>();
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<Thread>(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<e.length; i++) {
str.append(" at ");
str.append(e[i]);
str.append('\n');
}
return str.toString();
}
public void dumpAllThreadStacks() {
for (WeakReference<Thread> 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;});
}
}