server: fixed server app freeze on another instance already running, improved threads usage (related to #11285);

This commit is contained in:
Oleg Agafonov 2024-06-23 15:58:25 +04:00
parent f0c38cdb87
commit 7d675de876
21 changed files with 203 additions and 106 deletions

View file

@ -0,0 +1,42 @@
package mage.util;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Helper class to give debug friendly names for a threads
*
* @author JayDi85
*/
public class XMageThreadFactory implements ThreadFactory {
private final String prefix;
private final AtomicInteger counter = new AtomicInteger();
private final boolean isDaemon;
public XMageThreadFactory(String prefix) {
this(prefix, true);
}
/**
* @param prefix thread's starting name (can be changed by thread itself later)
* @param isDaemon mark thread as daemon on non-writeable tasks (e.g. can be terminated at any time without data loss)
*/
public XMageThreadFactory(String prefix, boolean isDaemon) {
this.prefix = prefix;
this.isDaemon = isDaemon;
}
@Override
public Thread newThread(Runnable r) {
int instanceNumber = this.counter.incrementAndGet();
Thread thread = new Thread(r);
thread.setDaemon(this.isDaemon);
// gives default name, but threads can change it by Thread.currentThread().setName (example: on game or tourney start)
thread.setName(String.format("%s - %d", this.prefix, instanceNumber));
return thread;
}
}