Fixed multithreading locking issues with posting commands

This commit is contained in:
lieght
2025-09-16 23:20:54 +02:00
parent c52311bedc
commit c6bf97aaa3
5 changed files with 9 additions and 80 deletions

View File

@@ -18,10 +18,6 @@ public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
// TcpServer server = new TcpServer(5001);
// Thread serverThread = new Thread(server);
// serverThread.start();
initSystems();
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
@@ -36,46 +32,11 @@ public class Main {
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest(serverId, "John", "Pim", ticTacToeGame));
String ticTacToeGameId = ticTacToeGame.get();
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId));
GlobalEventBus.post(new Events.ServerEvents.Command(connectionId, "MOVE", "0"));
GlobalEventBus.post(new Events.ServerEvents.Command(connectionId, "MOVE", "1"));
GlobalEventBus.post(new Events.ServerEvents.Command(connectionId, "MOVE", "2"));
GlobalEventBus.post(new Events.ServerEvents.Command(connectionId, "MOVE", "3"));
GlobalEventBus.post(new Events.ServerEvents.Command(connectionId, "MOVE", "4"));
GlobalEventBus.post(new Events.ServerEvents.Command(connectionId, "MOVE", "5"));
// GlobalEventBus.post(new Events.ServerEvents.EndTicTacToeGame(serverId, ticTacToeGameId));
// for (int i = 0; i < 1; i++) {
// Thread thread = new Thread(() -> {
//// logger.info("Server ID: {}", serverId);
// GlobalEventBus.post(new Events.ServerEvents.Command(serverId, "HELP", "TEST"));
// });
// thread.start();
// }
// GlobalEventBus.post(new Events.ServerEvents.ForceCloseAllConnections());
// GlobalEventBus.post(new Events.ServerEvents.ForceCloseAllServers());
//
// CompletableFuture<String> future2 = new CompletableFuture<>();
// GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest("127.0.0.1", "5001", future2));
// String serverId2 = future.get();
// logger.info("Server ID: {}", serverId2);
// GlobalEventBus.post(new Events.ServerEvents.Command(serverId2, "HELP", "TEST2"));
// GlobalEventBus.post(new Events.ServerEvents.StartConnection("127.0.0.1", "5001"));
// Server.startNew("127.0.0.1", "5001");
// Testsss.start(""); // Used for testing server.
// Window.start("");
// CompletableFuture<String> future6 = new CompletableFuture<>();
// GlobalEventBus.post(new Events.ServerEvents.RequestsAllConnections(future6));
// String serverConnections = future6.get();
// logger.info("Running connections: {}", serverConnections);
for (int x = 0; x < 100; x++) {
GlobalEventBus.post(new Events.ServerEvents.Command(connectionId, "MOVE", "" + x));
}
}
public static void initSystems() {

View File

@@ -10,8 +10,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
import static java.lang.Thread.sleep;
public class TcpServer implements Runnable {
protected static final Logger logger = LogManager.getLogger(TcpServer.class);
@@ -120,11 +118,6 @@ public class TcpServer implements Runnable {
new Thread(() -> {
for (int i = 0; i < this.RETRY_ATTEMPTS; i++) {
if (this.receivedQueue.offer(finalMessage)) break;
try {
sleep(this.WAIT_TIME);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
}

View File

@@ -1,27 +0,0 @@
package org.toop.server.backend;
import org.toop.eventbus.Events;
import org.toop.eventbus.GlobalEventBus;
public class Testsss extends Thread {
public Testsss() {}
// public void run() {
// while (true) {
// try {
// sleep(100);
// GlobalEventBus.post(new Events.ServerEvents.command("HELP", "TEST"));
// sleep(1000);
// GlobalEventBus.post(new Events.ServerEvents.ChangeConnection("127.0.0.1", "5001"));
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }
// }
public static void start(String keepEmpty) {
new Testsss().start();
}
}

View File

@@ -58,7 +58,9 @@ public class TicTacToeServer extends TcpServer {
public void gameManagerThread() {
while (true) { // TODO: Very heavy on thread
try {
wait(250);
synchronized (this) {
wait(250);
} // Fixes current thread is not owner.
} catch (InterruptedException e) {
logger.error("Interrupted", e);
}

View File

@@ -41,11 +41,11 @@ public class TicTacToe extends GameBase implements Runnable {
// command = this.parseCommand(command).toString();
// if (command == null) { continue; }
if (commandQueue.poll() != null) {
logger.info(commandQueue.poll());
if (commandQueue.poll() == null) {
continue;
}
// TODO: Game
// TODO: Game use the commandQueue to get the commands.
}
}