From 7b2206580a1013d2960c7ab7eadf7fde475c9ea6 Mon Sep 17 00:00:00 2001 From: zaxar163 Date: Sat, 10 Nov 2018 22:09:17 +0300 Subject: [PATCH] ThreadCount #48 --- .../java/ru/gravit/launchserver/LaunchServer.java | 11 +++++++++-- .../launchserver/socket/ServerSocketHandler.java | 13 +++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index ddc82673..f0a59f02 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -66,7 +66,6 @@ public final class LaunchServer implements Runnable, AutoCloseable { public static final class Config extends ConfigObject { - public final int port; // Handlers & Providers @@ -80,7 +79,10 @@ public static final class Config extends ConfigObject { public final HWIDHandler hwidHandler; // Misc options - + public final int threadCount; + + public final int threadCoreCount; + public final ExeConf launch4j; public final SignConf sign; @@ -111,6 +113,11 @@ private Config(BlockConfigEntry block, Path coredir, LaunchServer server) { address = block.getEntry("address", StringConfigEntry.class); port = VerifyHelper.verifyInt(block.getEntryValue("port", IntegerConfigEntry.class), VerifyHelper.range(0, 65535), "Illegal LaunchServer port"); + threadCoreCount = block.hasEntry("threadCoreCacheSize") ? VerifyHelper.verifyInt(block.getEntryValue("threadCoreCacheSize", IntegerConfigEntry.class), + VerifyHelper.range(2, 100), "Illegal LaunchServer thread cache size") : 0; + int internalThreadCount = block.hasEntry("threadCacheSize") ? VerifyHelper.verifyInt(block.getEntryValue("threadCacheSize", IntegerConfigEntry.class), + VerifyHelper.range(2, 100), "Illegal LaunchServer thread cache size") : (JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() >= 4 ? (JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() >= 6 ? JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() / 2 : JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors()) : JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() / 2); + threadCount = threadCoreCount > internalThreadCount ? threadCoreCount : internalThreadCount; authRateLimit = VerifyHelper.verifyInt(block.getEntryValue("authRateLimit", IntegerConfigEntry.class), VerifyHelper.range(0, 1000000), "Illegal authRateLimit"); authRateLimitMilis = VerifyHelper.verifyInt(block.getEntryValue("authRateLimitMilis", IntegerConfigEntry.class), diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java index 7ff3825f..46b25989 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java @@ -5,8 +5,10 @@ import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; @@ -35,7 +37,7 @@ public interface Listener { // Instance private final LaunchServer server; private final AtomicReference serverSocket = new AtomicReference<>(); - private final ExecutorService threadPool = Executors.newCachedThreadPool(THREAD_FACTORY); + private final ExecutorService threadPool; public final SessionManager sessionManager; private final AtomicLong idCounter = new AtomicLong(0L); @@ -43,13 +45,16 @@ public interface Listener { private volatile Listener listener; public ServerSocketHandler(LaunchServer server) { - this.server = server; - sessionManager = new SessionManager(); + this(server, new SessionManager()); GarbageManager.registerNeedGC(sessionManager); } public ServerSocketHandler(LaunchServer server, SessionManager sessionManager) { this.server = server; + threadPool = new ThreadPoolExecutor(server.config.threadCoreCount, Integer.MAX_VALUE, + server.config.threadCount, TimeUnit.SECONDS, + new SynchronousQueue(), + THREAD_FACTORY); this.sessionManager = sessionManager; }