From 6293ba909b8bf0f4a964a988dfcd4fcff1876476 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 30 Oct 2024 21:18:15 +0200 Subject: [PATCH] Fixed SonarCloud warnings --- .../java/com/github/underscore/Underscore.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/github/underscore/Underscore.java b/src/main/java/com/github/underscore/Underscore.java index fa68b225..2a26fbc0 100644 --- a/src/main/java/com/github/underscore/Underscore.java +++ b/src/main/java/com/github/underscore/Underscore.java @@ -2098,13 +2098,14 @@ public static java.util.concurrent.ScheduledFuture delay( final Supplier function, final int delayMilliseconds) { final java.util.concurrent.ScheduledExecutorService scheduler = java.util.concurrent.Executors.newSingleThreadScheduledExecutor(); - final java.util.concurrent.ScheduledFuture future = - scheduler.schedule( - function::get, - delayMilliseconds, - java.util.concurrent.TimeUnit.MILLISECONDS); - scheduler.shutdown(); - return future; + try { + return scheduler.schedule( + function::get, + delayMilliseconds, + java.util.concurrent.TimeUnit.MILLISECONDS); + } finally { + scheduler.shutdown(); + } } public static java.util.concurrent.ScheduledFuture defer(final Supplier function) { @@ -3624,6 +3625,7 @@ public static java.util.concurrent.ScheduledFuture setInterval( final Supplier function, final int delayMilliseconds) { final java.util.concurrent.ScheduledExecutorService scheduler = java.util.concurrent.Executors.newSingleThreadScheduledExecutor(); + Runtime.getRuntime().addShutdownHook(new Thread(scheduler::shutdown)); return scheduler.scheduleAtFixedRate( function::get, delayMilliseconds,