File tree 1 file changed +9
-7
lines changed
src/main/java/com/github/underscore
1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change @@ -2098,13 +2098,14 @@ public static <T> java.util.concurrent.ScheduledFuture<T> delay(
2098
2098
final Supplier <T > function , final int delayMilliseconds ) {
2099
2099
final java .util .concurrent .ScheduledExecutorService scheduler =
2100
2100
java .util .concurrent .Executors .newSingleThreadScheduledExecutor ();
2101
- final java .util .concurrent .ScheduledFuture <T > future =
2102
- scheduler .schedule (
2103
- function ::get ,
2104
- delayMilliseconds ,
2105
- java .util .concurrent .TimeUnit .MILLISECONDS );
2106
- scheduler .shutdown ();
2107
- return future ;
2101
+ try {
2102
+ return scheduler .schedule (
2103
+ function ::get ,
2104
+ delayMilliseconds ,
2105
+ java .util .concurrent .TimeUnit .MILLISECONDS );
2106
+ } finally {
2107
+ scheduler .shutdown ();
2108
+ }
2108
2109
}
2109
2110
2110
2111
public static <T > java .util .concurrent .ScheduledFuture <T > defer (final Supplier <T > function ) {
@@ -3624,6 +3625,7 @@ public static <T> java.util.concurrent.ScheduledFuture setInterval(
3624
3625
final Supplier <T > function , final int delayMilliseconds ) {
3625
3626
final java .util .concurrent .ScheduledExecutorService scheduler =
3626
3627
java .util .concurrent .Executors .newSingleThreadScheduledExecutor ();
3628
+ Runtime .getRuntime ().addShutdownHook (new Thread (scheduler ::shutdown ));
3627
3629
return scheduler .scheduleAtFixedRate (
3628
3630
function ::get ,
3629
3631
delayMilliseconds ,
You can’t perform that action at this time.
0 commit comments