From 5117ecaecb48b45f6929efc1198b283bdd082804 Mon Sep 17 00:00:00 2001 From: Half_nothing Date: Thu, 25 Apr 2024 22:49:23 +0800 Subject: [PATCH] fix(concurrent): The issue of singleton mode invalidating during multi-threaded concurrency is fixed --- .../harryh/arkpets/concurrent/ProcessPool.java | 9 ++++++--- .../arkpets/concurrent/SocketServer.java | 18 ++++++++++++++---- desktop/src/cn/harryh/arkpets/ArkHomeFX.java | 4 ++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/core/src/cn/harryh/arkpets/concurrent/ProcessPool.java b/core/src/cn/harryh/arkpets/concurrent/ProcessPool.java index e656a588..5acb0de1 100644 --- a/core/src/cn/harryh/arkpets/concurrent/ProcessPool.java +++ b/core/src/cn/harryh/arkpets/concurrent/ProcessPool.java @@ -22,11 +22,14 @@ public final class ProcessPool implements Executor { return thread; }); - private static ProcessPool instance = null; + private static volatile ProcessPool instance = null; - public static synchronized ProcessPool getInstance() { + public static ProcessPool getInstance() { if (instance == null) - instance = new ProcessPool(); + synchronized (ProcessPool.class) { + if (instance == null) + instance = new ProcessPool(); + } return instance; } diff --git a/core/src/cn/harryh/arkpets/concurrent/SocketServer.java b/core/src/cn/harryh/arkpets/concurrent/SocketServer.java index 94630c8e..34159c62 100644 --- a/core/src/cn/harryh/arkpets/concurrent/SocketServer.java +++ b/core/src/cn/harryh/arkpets/concurrent/SocketServer.java @@ -15,6 +15,7 @@ import java.util.UUID; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.atomic.AtomicBoolean; import static cn.harryh.arkpets.Const.serverPorts; @@ -24,12 +25,15 @@ public final class SocketServer { private ServerSocket serverSocket = null; private final Set sessionList = new CopyOnWriteArraySet<>(); private Thread listener; + private final AtomicBoolean running = new AtomicBoolean(false); + private static volatile SocketServer instance = null; - private static SocketServer instance = null; - - public static synchronized SocketServer getInstance() { + public static SocketServer getInstance() { if (instance == null) - instance = new SocketServer(); + synchronized (SocketServer.class) { + if (instance == null) + instance = new SocketServer(); + } return instance; } @@ -43,6 +47,8 @@ private SocketServer() { */ public synchronized void startServer(HostTray hostTray) throws PortUtils.NoPortAvailableException, PortUtils.ServerCollisionException { + if (running.get()) + return; Logger.info("SocketServer", "Request to start server"); this.port = PortUtils.getAvailablePort(serverPorts); listener = new Thread(() -> { @@ -65,15 +71,19 @@ public synchronized void startServer(HostTray hostTray) } }); ProcessPool.getInstance().execute(listener); + running.set(true); } /** Stops the server and close all the sessions. */ public synchronized void stopServer() { + if (!running.get()) + return; Logger.info("SocketServer", "Request to stop server"); if (listener != null) listener.interrupt(); sessionList.forEach(SocketSession::close); + running.set(false); } @Override diff --git a/desktop/src/cn/harryh/arkpets/ArkHomeFX.java b/desktop/src/cn/harryh/arkpets/ArkHomeFX.java index 36cd8df3..f88438b0 100644 --- a/desktop/src/cn/harryh/arkpets/ArkHomeFX.java +++ b/desktop/src/cn/harryh/arkpets/ArkHomeFX.java @@ -134,6 +134,10 @@ public void stop() { Logger.debug("Launcher", "Finished stopping"); } + public void exit() { + rootModule.exit(); + } + public void popLoading(EventHandler handler) { rootModule.popLoading(handler); }