Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions core/runtime/src/main/java/io/quarkus/runtime/ExecutorRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -77,29 +76,31 @@ private static Runnable createShutdownTask(ThreadPoolConfig threadPoolConfig, En
@Override
public void run() {
executor.shutdown();
final Duration shutdownTimeout = threadPoolConfig.shutdownTimeout();
final Optional<Duration> optionalInterval = threadPoolConfig.shutdownCheckInterval();
long remaining = shutdownTimeout.toNanos();
final long interval = optionalInterval.orElse(Duration.ofNanos(Long.MAX_VALUE)).toNanos();
long intervalRemaining = interval;
long interruptRemaining = threadPoolConfig.shutdownInterrupt().toNanos();
final long configShutdownTimeout = threadPoolConfig.shutdownTimeout().toNanos();
final long configShutdownInterrupt = threadPoolConfig.shutdownInterrupt().toNanos();
final long configShutdownCheckInterval = threadPoolConfig.shutdownCheckInterval()
.orElse(Duration.ofNanos(Long.MAX_VALUE)).toNanos();
long shutdownTimeout = configShutdownTimeout;
long shutdownInterrupt = configShutdownInterrupt;
long shutdownCheckInterval = configShutdownCheckInterval;

long start = System.nanoTime();
int loop = 1;
for (;;) {
// This log can be very useful when debugging problems
log.debugf("loop: %s, remaining: %s, intervalRemaining: %s, interruptRemaining: %s", loop++, remaining,
intervalRemaining, interruptRemaining);
log.debugf("loop: %s, shutdownTimeout: %s, shutdownCheckInterval: %s, shutdownInterrupt: %s", loop++,
shutdownTimeout, shutdownCheckInterval, shutdownInterrupt);
try {
if (!executor.awaitTermination(Math.min(remaining, intervalRemaining), TimeUnit.NANOSECONDS)) {
if (!executor.awaitTermination(Math.min(shutdownTimeout, shutdownCheckInterval),
TimeUnit.NANOSECONDS)) {
long elapsed = System.nanoTime() - start;
intervalRemaining -= elapsed;
remaining -= elapsed;
interruptRemaining -= elapsed;
if (interruptRemaining <= 0) {
shutdownTimeout = configShutdownTimeout - elapsed;
shutdownInterrupt = configShutdownInterrupt - elapsed;
shutdownCheckInterval = configShutdownCheckInterval - elapsed;
if (shutdownInterrupt <= 0) {
executor.shutdown(true);
}
if (remaining <= 0) {
if (shutdownTimeout <= 0) {
// done waiting
final List<Runnable> runnables = executor.shutdownNow();
if (!runnables.isEmpty()) {
Expand All @@ -111,8 +112,8 @@ public void run() {
}
break;
}
if (intervalRemaining <= 0) {
intervalRemaining = interval;
if (shutdownCheckInterval <= 0) {
shutdownCheckInterval = configShutdownCheckInterval;
// do some probing
final int queueSize = executor.getQueueSize();
final Thread[] runningThreads = executor.getRunningThreads();
Expand Down
Loading