releaseNow when trying to shutdown service

This commit is contained in:
mcrcortex
2025-05-02 23:44:12 +10:00
parent f74992a37a
commit 917e308ece

View File

@@ -24,6 +24,7 @@ public class ServiceThreadPool {
} }
private volatile boolean running = true; private volatile boolean running = true;
private volatile boolean releaseNow = false;
private Thread[] workers = new Thread[0]; private Thread[] workers = new Thread[0];
private final Semaphore jobCounter = new Semaphore(0); private final Semaphore jobCounter = new Semaphore(0);
@@ -129,9 +130,11 @@ public class ServiceThreadPool {
void steal(ServiceSlice service, int count) { void steal(ServiceSlice service, int count) {
this.totalJobWeight.addAndGet(-(service.weightPerJob*(long)count)); this.totalJobWeight.addAndGet(-(service.weightPerJob*(long)count));
this.releaseNow = true;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
this.jobCounter.acquireUninterruptibly(); this.jobCounter.acquireUninterruptibly();
} }
this.releaseNow = false;
} }
private void worker(int threadId) { private void worker(int threadId) {
@@ -157,6 +160,15 @@ public class ServiceThreadPool {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
if (this.releaseNow) {
this.jobCounter.release();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
break;
}
var ref = this.serviceSlices; var ref = this.serviceSlices;
if (ref.length == 0) { if (ref.length == 0) {
Logger.error("Service worker tried to run but had 0 slices"); Logger.error("Service worker tried to run but had 0 slices");