From ab4bbdb5f37ae11cb42fc03466fa12482db2b86e Mon Sep 17 00:00:00 2001 From: AlexisDrogoul Date: Mon, 23 Oct 2023 13:05:57 +0700 Subject: [PATCH] Implementation proposal for #3921. Please test and report ! --- .../msi/gama/runtime/server/CommandExecutor.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/msi.gama.core/src/msi/gama/runtime/server/CommandExecutor.java b/msi.gama.core/src/msi/gama/runtime/server/CommandExecutor.java index 046ab58c74..e1e5a4963e 100644 --- a/msi.gama.core/src/msi/gama/runtime/server/CommandExecutor.java +++ b/msi.gama.core/src/msi/gama/runtime/server/CommandExecutor.java @@ -26,10 +26,9 @@ import static msi.gama.runtime.server.ISocketCommand.UPLOAD; import java.util.AbstractMap; -import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; -import java.util.Queue; +import java.util.concurrent.LinkedBlockingQueue; import org.java_websocket.WebSocket; import org.java_websocket.enums.ReadyState; @@ -56,14 +55,17 @@ public class CommandExecutor { protected final Map commands; /** The command queue. */ - protected volatile Queue>> commandQueue; + protected volatile LinkedBlockingQueue>> commandQueue; /** The command execution thread. */ protected final Thread commandExecutionThread = new Thread(() -> { while (true) { - while (!commandQueue.isEmpty()) { - var cmd = commandQueue.poll(); + Entry> cmd; + try { + cmd = commandQueue.take(); process(cmd.getKey(), cmd.getValue()); + } catch (InterruptedException e) { + e.printStackTrace(); } } }); @@ -77,7 +79,7 @@ public class CommandExecutor { public CommandExecutor() { commands = GAMA.getGui().getServerCommands(); - commandQueue = new LinkedList<>(); + commandQueue = new LinkedBlockingQueue<>(); commandExecutionThread.setUncaughtExceptionHandler(GamaExecutorService.EXCEPTION_HANDLER); commandExecutionThread.start(); } @@ -93,7 +95,7 @@ public CommandExecutor() { * @date 15 oct. 2023 */ public void pushCommand(final WebSocket socket, final IMap map) { - commandQueue.add(new AbstractMap.SimpleEntry<>(socket, map)); + commandQueue.offer(new AbstractMap.SimpleEntry<>(socket, map)); } /**