NoIssue: Exit synchronization fix

This commit is contained in:
2026-01-28 12:35:19 +01:00
parent a9411f1d37
commit 661ab292ad

View File

@@ -13,12 +13,17 @@ import java.util.concurrent.atomic.AtomicInteger;
public class KickerImpl implements Kicker {
static void main() {
static void main() throws Exception {
new KickerImpl();
}
public KickerImpl() {
public KickerImpl() throws InterruptedException {
startProcess("com.r35157.nenjim.kicker.impl.ref.Dummy");
System.out.println("Waiting for all processes to finish!");
awaitAllProcessesFinished();
log.info("Kicker successfully shutdown!");
System.out.println("Kicker successfully shutdown!");
}
@Override
@@ -49,21 +54,25 @@ public class KickerImpl implements Kicker {
proc.setContext(context);
proc.setHub(null); log.warn("TODO: Set the HUB here when we have one!");
System.out.println("@@@1");
synchronized (lock) {
running++;
}
Thread t = Thread.ofVirtual()
.name(procName)
.start(() -> {
System.out.println("@@@2");
try {
log.info("Starting process '{}' (#{})...", procName, processId);
System.out.println("@@@3");
proc.run();
System.out.println("@@@4");
} catch (Throwable e) {
log.error("Process '{}' (#{}) crashed!", procName, processId, e);
} finally {
processThreads.remove(processId);
processes.remove(processId);
synchronized (lock) {
running--;
lock.notifyAll();
}
}
});
@@ -79,8 +88,18 @@ public class KickerImpl implements Kicker {
public void noop() {
}
public void awaitAllProcessesFinished() throws InterruptedException {
synchronized (lock) {
while (running > 0) {
lock.wait();
}
}
}
private static final Logger log = LogManager.getLogger(KickerImpl.class);
private final Object lock = new Object();
private int running = 0;
private final ConcurrentHashMap<Integer, NenjimProcess> processes = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Integer, Thread> processThreads = new ConcurrentHashMap<>();
private final AtomicInteger nextProcessId = new AtomicInteger(1);