Aller au contenu

Executor Service Leak (ARCH4040)

Ce contenu n’est pas encore disponible dans votre langue.

Key java.async.executor-service-leak · Severity warning · Category async

An ExecutorService created via Executors.newFixedThreadPool and never shut down leaks non-daemon threads and can prevent the JVM from exiting.

void run(List<Task> tasks) {
ExecutorService pool = Executors.newFixedThreadPool(4);
tasks.forEach(t -> pool.submit(t)); // never shut down
}
void run(List<Task> tasks) {
ExecutorService pool = Executors.newFixedThreadPool(4);
try {
tasks.forEach(pool::submit);
} finally {
pool.shutdown();
}
}

Shut the ExecutorService down with shutdown() in a finally block, or use try-with-resources.