From 2995564cdf8b07255fa1a76b68d4166d154e86e3 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Sun, 6 Jun 2021 19:30:17 +0200 Subject: [PATCH] feat: switched LobbyRunner implementation to threads again --- .../server/lobbymanager/LobbyRunner.java | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunner.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunner.java index de402c8..edb1dfb 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunner.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyRunner.java @@ -28,12 +28,13 @@ public class LobbyRunner { } - private final ExecutorService lobbyExecutor; - private final HashSet activeLobbies; + private final HashMap activeLobbies; + /** + * Constructs a new LobbyRunner + */ private LobbyRunner() { - this.lobbyExecutor = Executors.newFixedThreadPool(Server.getMaxLobbies()); - this.activeLobbies = new HashSet<>(); + this.activeLobbies = new HashMap<>(); } boolean canAddLobby() { @@ -43,22 +44,26 @@ public class LobbyRunner { /** Starts a new thread for the current LobbyConnection, and adds it to the currently active lobbies */ void startLobby(LobbyConnection connection) { Logger.trace("Starting lobby connection thread '{}'", connection.gameID); - if (activeLobbies.contains(connection)) { - Logger.warn("Already active lobby was started again. This is probably a bug."); - } else if (!canAddLobby()) { - Logger.warn("Scheduling lobby to be started while no thread is free. This might be a bug."); - activeLobbies.add(connection); - lobbyExecutor.execute(connection); - } else { - Logger.trace("Executing LobbyThread 'Lobby-{}'...", connection.gameID); - activeLobbies.add(connection); - lobbyExecutor.execute(connection); + synchronized (activeLobbies) { + if (activeLobbies.containsKey(connection)) { + Logger.warn("Already active lobby was started again. This is probably a bug."); + } else if (!canAddLobby()) { + Logger.warn("Scheduling lobby to be started while max lobbies is reached. This might be a bug."); + Thread lobbyThread = new Thread(connection, "Lobby-" + connection.gameID); + activeLobbies.put(connection, lobbyThread); + lobbyThread.start(); + } else { + Logger.trace("Executing LobbyThread 'Lobby-{}'...", connection.gameID); + Thread lobbyThread = new Thread(connection, "Lobby-" + connection.gameID); + activeLobbies.put(connection, lobbyThread); + lobbyThread.start(); + } } } void removeLobby(LobbyConnection lobby) { synchronized (activeLobbies) { - if (!activeLobbies.contains(lobby)) { + if (!activeLobbies.containsKey(lobby)) { Logger.warn("Tried to remove non-existent lobby thread. This is probably a bug."); } else { Logger.debug("Stopping and removing lobby '{}'", lobby.gameID); @@ -69,18 +74,17 @@ public class LobbyRunner { } boolean isStarted(LobbyConnection connection) { - return activeLobbies.contains(connection); + return activeLobbies.containsKey(connection); } /** Shutdown all threads, destroy the lobbies, and close everything up */ void shutdownAll() { Logger.info("Stopping and removing all LobbyThreads"); - activeLobbies.forEach(LobbyConnection::terminateConnection); - try { - lobbyExecutor.awaitTermination(5, TimeUnit.SECONDS); - } catch (InterruptedException e) { - e.printStackTrace(); - } - lobbyExecutor.shutdownNow(); + activeLobbies.keySet().forEach(LobbyConnection::terminateConnection); + Logger.debug("All lobby shutdown flags set"); + } + + // later... + void checkThreads() { } }