Skip to content

Commit 1090fe6

Browse files
committed
refactor(ServerController): implement custom thread factory for thread pool
Add DefaultThreadFactory to provide better thread naming and consistent thread properties. This improves debugging and monitoring capabilities by making thread origins more identifiable.
1 parent 2d5dfeb commit 1090fe6

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/main/java/mindustrytool/ServerController.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import java.util.concurrent.Executors;
77
import java.util.concurrent.ScheduledExecutorService;
88
import java.util.concurrent.SynchronousQueue;
9+
import java.util.concurrent.ThreadFactory;
910
import java.util.concurrent.ThreadPoolExecutor;
1011
import java.util.concurrent.TimeUnit;
12+
import java.util.concurrent.atomic.AtomicInteger;
1113

1214
import org.pf4j.Plugin;
1315

@@ -57,7 +59,36 @@ public class ServerController extends Plugin implements MindustryToolPlugin {
5759
20,
5860
5,
5961
TimeUnit.SECONDS,
60-
new SynchronousQueue<Runnable>());
62+
new SynchronousQueue<Runnable>(),
63+
new DefaultThreadFactory());
64+
65+
private class DefaultThreadFactory implements ThreadFactory {
66+
private static final AtomicInteger poolNumber = new AtomicInteger(1);
67+
private final ThreadGroup group;
68+
private final AtomicInteger threadNumber = new AtomicInteger(1);
69+
private final String namePrefix;
70+
71+
@SuppressWarnings("deprecation")
72+
DefaultThreadFactory() {
73+
@SuppressWarnings("removal")
74+
SecurityManager s = System.getSecurityManager();
75+
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
76+
namePrefix = "background-pool-" +
77+
poolNumber.getAndIncrement() +
78+
"-thread-";
79+
}
80+
81+
public Thread newThread(Runnable r) {
82+
Thread t = new Thread(group, r,
83+
namePrefix + threadNumber.getAndIncrement(),
84+
0);
85+
if (t.isDaemon())
86+
t.setDaemon(false);
87+
if (t.getPriority() != Thread.NORM_PRIORITY)
88+
t.setPriority(Thread.NORM_PRIORITY);
89+
return t;
90+
}
91+
}
6192

6293
public final ScheduledExecutorService BACKGROUND_SCHEDULER = Executors
6394
.newSingleThreadScheduledExecutor();

0 commit comments

Comments
 (0)