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,