forked from External/mage
server: fixed server app freeze on another instance already running, improved threads usage (related to #11285);
This commit is contained in:
parent
f0c38cdb87
commit
7d675de876
21 changed files with 203 additions and 106 deletions
42
Mage/src/main/java/mage/util/XMageThreadFactory.java
Normal file
42
Mage/src/main/java/mage/util/XMageThreadFactory.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue