Skip to content

Commit 0ca8f6f

Browse files
committed
fix: TaskExecutor.close() shutdown is very fast now even with NonBlockingLineWriter Task
1 parent 86bbbb9 commit 0ca8f6f

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/dev/enola/be/io/NonBlockingLineWriter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ protected void executeIt() throws Exception {
4141
delegate.println(line);
4242
}
4343
} catch (InterruptedException e) {
44+
// We've been interrupted, which is the signal to shut down.
45+
// But before we exit, let's process any remaining messages in the queue.
46+
Object line;
47+
while ((line = queue.poll()) != null) {
48+
delegate.println(line);
49+
}
50+
// Re-set the interrupt flag to be a good citizen.
4451
Thread.currentThread().interrupt();
4552
}
4653
}

src/dev/enola/be/task/TaskExecutor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ public Set<UUID> list() {
122122

123123
@Override
124124
public void close() {
125+
// Signal to all running tasks, so they can terminate gracefully & fast
126+
for (Task<?, ?> task : tasks.values()) {
127+
task.cancel();
128+
}
129+
125130
TaskExecutorServices.close(executor);
126131
TaskExecutorServices.close(timeoutScheduler);
127132
}

src/dev/enola/be/task/TaskExecutorServices.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ static void close(ExecutorService executor) {
4848
var start = System.currentTimeMillis();
4949
logger.fine(() -> "Starting to shut down ExecutorService " + executor);
5050

51-
// TODO Signal close() to all running tasks, so they can terminate gracefully & fast
52-
5351
// TODO Then (only) use Java 19+ executor.close()
5452

5553
executor.shutdown();

0 commit comments

Comments
 (0)