private static CompletableFuture<Void> addClosingTask(
            AutoCloseable autoCloseable, @Nullable Runnable whenClosing, String name) {
        final CompletableFuture<Void> closeFuture = new CompletableFuture<>();
        final Runnable task = () -> {
            if (whenClosing != null) {
                try {
                    whenClosing.run();
                } catch (Exception e) {
                    logger.warn("Unexpected exception while running a shutdown callback:", e);
                }
            }
            try {
                autoCloseable.close();
                logger.debug("{} has been closed.", name);
                closeFuture.complete(null);
            } catch (Throwable cause) {
                logger.warn("Unexpected exception while closing a {}.", name, cause);
                closeFuture.completeExceptionally(cause);
            }
        };

        reentrantLock.lock();
        try {
            final Queue<Runnable> onShutdownTasks =
                    autoCloseableOnShutdownTasks.computeIfAbsent(autoCloseable, key -> new ArrayDeque<>());
            onShutdownTasks.add(task);
            if (!addedShutdownHook) {
                Runtime.getRuntime().addShutdownHook(THREAD_FACTORY.newThread(() -> {
                    reentrantLock.lock();
                    try {
                        autoCloseableOnShutdownTasks.forEach((factory, queue) -> {
                            for (;;) {
                                final Runnable onShutdown = queue.poll();
                                if (onShutdown == null) {
                                    break;
                                } else {
                                    onShutdown.run();
                                }
                            }
                        });
                    } finally {
                        reentrantLock.unlock();
                    }
                }));
                addedShutdownHook = true;
            }
        } finally {
            reentrantLock.unlock();
        }
        return closeFuture;
    }
Adds an {@link AutoCloseable} and a {@link Runnable} to the JVM shutdown hook.
core/src/main/java/com/linecorp/armeria/common/util/ShutdownHooks.java