Last active
June 21, 2022 13:22
-
-
Save Shilo/d619945b8f53672a585f68bc15fda241 to your computer and use it in GitHub Desktop.
Java mimic Javascript timeout.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.xida; | |
import java.util.concurrent.ScheduledFuture; | |
import java.util.concurrent.TimeUnit; | |
public class Utils { | |
public static ScheduledFuture<?> setTimeout(long delayMilliseconds, Runnable runnable) { | |
var executor = Executors.newSingleThreadScheduledExecutor(); | |
return executor.schedule(runnable, delayMilliseconds, TimeUnit.MILLISECONDS); | |
} | |
public static boolean cancelTimeout(ScheduledFuture<?> scheduledFuture) { | |
if (scheduledFuture == null) | |
return false; | |
return scheduledFuture.cancel(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment