Skip to content

Commit

Permalink
fix(concurrent): The issue of singleton mode invalidating during mult…
Browse files Browse the repository at this point in the history
…i-threaded concurrency is fixed
  • Loading branch information
half-nothing committed Apr 26, 2024
1 parent 6cbf7b2 commit 5117eca
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
9 changes: 6 additions & 3 deletions core/src/cn/harryh/arkpets/concurrent/ProcessPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 14 additions & 4 deletions core/src/cn/harryh/arkpets/concurrent/SocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,12 +25,15 @@ public final class SocketServer {
private ServerSocket serverSocket = null;
private final Set<SocketSession> 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;
}

Expand All @@ -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(() -> {
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions desktop/src/cn/harryh/arkpets/ArkHomeFX.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public void stop() {
Logger.debug("Launcher", "Finished stopping");
}

public void exit() {
rootModule.exit();
}

public void popLoading(EventHandler<ActionEvent> handler) {
rootModule.popLoading(handler);
}
Expand Down

0 comments on commit 5117eca

Please sign in to comment.