2025-02-07 11:38:12 -06:00
|
|
|
package engine.job;
|
|
|
|
|
|
2025-02-07 12:08:06 -06:00
|
|
|
import engine.server.world.WorldServer;
|
2025-02-07 11:38:12 -06:00
|
|
|
import org.pmw.tinylog.Logger;
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
|
|
|
|
|
|
public class JobThread implements Runnable {
|
|
|
|
|
private final AbstractJob currentJob;
|
|
|
|
|
private final ReentrantLock lock = new ReentrantLock();
|
|
|
|
|
|
2025-02-07 12:08:06 -06:00
|
|
|
private static Long nextThreadPrint;
|
|
|
|
|
|
2025-02-07 11:38:12 -06:00
|
|
|
public JobThread(AbstractJob job){
|
|
|
|
|
this.currentJob = job;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
if (this.currentJob != null) {
|
|
|
|
|
if (lock.tryLock(5, TimeUnit.SECONDS)) { // Timeout to prevent deadlock
|
|
|
|
|
try {
|
|
|
|
|
this.currentJob.doJob();
|
|
|
|
|
} finally {
|
|
|
|
|
lock.unlock();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Logger.warn("JobThread could not acquire lock in time, skipping job.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Logger.error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void startJobThread(AbstractJob job){
|
|
|
|
|
JobThread jobThread = new JobThread(job);
|
|
|
|
|
Thread thread = new Thread(jobThread);
|
2025-02-07 11:50:43 -06:00
|
|
|
thread.setName("JOB THREAD: " + job.getWorkerID());
|
2025-02-07 11:38:12 -06:00
|
|
|
thread.start();
|
2025-02-07 12:08:06 -06:00
|
|
|
|
|
|
|
|
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
|
2025-02-07 12:16:52 -06:00
|
|
|
if(threads.length > 30)
|
|
|
|
|
Logger.info("Total threads in application: " + threads.length + " / " + availableThreads);
|
2025-02-07 11:38:12 -06:00
|
|
|
}
|
|
|
|
|
}
|