From 29aea83217448c7b5fcc64517cb939c51e8dae9c Mon Sep 17 00:00:00 2001 From: FatBoy-DOTC Date: Fri, 7 Feb 2025 12:08:06 -0600 Subject: [PATCH] track threads being used --- src/engine/job/JobThread.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/engine/job/JobThread.java b/src/engine/job/JobThread.java index 6abd3f26..df36941f 100644 --- a/src/engine/job/JobThread.java +++ b/src/engine/job/JobThread.java @@ -1,5 +1,6 @@ package engine.job; +import engine.server.world.WorldServer; import org.pmw.tinylog.Logger; import java.util.concurrent.TimeUnit; @@ -9,6 +10,8 @@ public class JobThread implements Runnable { private final AbstractJob currentJob; private final ReentrantLock lock = new ReentrantLock(); + private static Long nextThreadPrint; + public JobThread(AbstractJob job){ this.currentJob = job; } @@ -37,5 +40,35 @@ public class JobThread implements Runnable { Thread thread = new Thread(jobThread); thread.setName("JOB THREAD: " + job.getWorkerID()); thread.start(); + + if(JobThread.nextThreadPrint == null){ + JobThread.nextThreadPrint = System.currentTimeMillis(); + }else{ + if(JobThread.nextThreadPrint < System.currentTimeMillis()){ + JobThread.tryPrintThreads(); + JobThread.nextThreadPrint = System.currentTimeMillis() + 10000L; + } + } + } + + public static void tryPrintThreads(){ + ThreadGroup rootGroup = Thread.currentThread().getThreadGroup(); + while (rootGroup.getParent() != null) { + rootGroup = rootGroup.getParent(); + } + + // Estimate the number of threads + int activeThreads = rootGroup.activeCount(); + + // Create an array to hold the threads + Thread[] threads = new Thread[activeThreads]; + + // Get the active threads + rootGroup.enumerate(threads, true); + + int availableThreads = Runtime.getRuntime().availableProcessors(); + + // Print the count + Logger.info("Total threads in application: " + threads.length + " / " + availableThreads); } }