NoIssue: Exit synchronization fix
This commit is contained in:
@@ -13,12 +13,17 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
|
|
||||||
public class KickerImpl implements Kicker {
|
public class KickerImpl implements Kicker {
|
||||||
|
|
||||||
static void main() {
|
static void main() throws Exception {
|
||||||
new KickerImpl();
|
new KickerImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
public KickerImpl() {
|
public KickerImpl() throws InterruptedException {
|
||||||
startProcess("com.r35157.nenjim.kicker.impl.ref.Dummy");
|
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
|
@Override
|
||||||
@@ -49,21 +54,25 @@ public class KickerImpl implements Kicker {
|
|||||||
proc.setContext(context);
|
proc.setContext(context);
|
||||||
proc.setHub(null); log.warn("TODO: Set the HUB here when we have one!");
|
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()
|
Thread t = Thread.ofVirtual()
|
||||||
.name(procName)
|
.name(procName)
|
||||||
.start(() -> {
|
.start(() -> {
|
||||||
System.out.println("@@@2");
|
|
||||||
try {
|
try {
|
||||||
log.info("Starting process '{}' (#{})...", procName, processId);
|
log.info("Starting process '{}' (#{})...", procName, processId);
|
||||||
System.out.println("@@@3");
|
|
||||||
proc.run();
|
proc.run();
|
||||||
System.out.println("@@@4");
|
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
log.error("Process '{}' (#{}) crashed!", procName, processId, e);
|
log.error("Process '{}' (#{}) crashed!", procName, processId, e);
|
||||||
} finally {
|
} finally {
|
||||||
processThreads.remove(processId);
|
processThreads.remove(processId);
|
||||||
processes.remove(processId);
|
processes.remove(processId);
|
||||||
|
synchronized (lock) {
|
||||||
|
running--;
|
||||||
|
lock.notifyAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -79,8 +88,18 @@ public class KickerImpl implements Kicker {
|
|||||||
public void noop() {
|
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 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, NenjimProcess> processes = new ConcurrentHashMap<>();
|
||||||
private final ConcurrentHashMap<Integer, Thread> processThreads = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<Integer, Thread> processThreads = new ConcurrentHashMap<>();
|
||||||
private final AtomicInteger nextProcessId = new AtomicInteger(1);
|
private final AtomicInteger nextProcessId = new AtomicInteger(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user